Discussion in 'Python' started by hi_roger, Sep 9, 2009.

1. ### hi_rogerGuest

i know how to select a submatrix using the slice object in numpy. But
how can i select a submatrix
A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
j1,j2,j3 , and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )
The submatrix must share data memory with original matrix.

Any one help? thank you

hi_roger, Sep 9, 2009

2. ### sturlamoldenGuest

On 9 Sep, 03:45, hi_roger <> wrote:

> i know how to select a submatrix using the slice object in numpy. But
> how can i select a submatrix
> A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
> j1,j2,j3 ,  and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )

You just pass an array of ints for each dimension. If you want a 3x3
submatrix, you must pass in two 3x3 arrays of indices.

> The submatrix must share data memory with original matrix.

That is the tricky part. An ndarray must be indexable using an array
of strides. That is in C:

void *get_element_ptr( PyArrayObject *a, int indices[])
{
char *out = a->data;
int d;
for (d=0; d < a->nd; d++)
out += indices[d] * a->strides[d];
return (void *)out;
}

If you slice with an array or list of ints in Python, the resulting
array cannot be indexed like this. Therefore NumPy is forced to make a
copy. So if I do

>>> import numpy as np
>>> a = np.zeros((10,10))
>>> b = a[[1,3,5],[6,7,8]]

I get this:

>>> b.flags['OWNDATA']

True

But:

>>> c = a[::2,::2]
>>> c.flags['OWNDATA']

False

This is because C can still be indexed with strides as shown above,

sturlamolden, Sep 9, 2009

3. ### sturlamoldenGuest

On 9 Sep, 03:45, hi_roger <> wrote:
>
> i know how to select a submatrix using the slice object in numpy. But
> how can i select a submatrix
> A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
> j1,j2,j3 ,  and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )
> The submatrix must share data memory with original matrix.

So the only way to do this is to make an ndarray subclass that
overloads __getitem__, __setitem__, and __iter__, and takes care of
the mapping into A. Thus you get a double indirection.

sturlamolden, Sep 9, 2009
4. ### Robert KernGuest

On 2009-09-08 20:45 PM, hi_roger wrote:
>
> i know how to select a submatrix using the slice object in numpy. But
> how can i select a submatrix
> A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
> j1,j2,j3 , and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )
> The submatrix must share data memory with original matrix.
>
> Any one help? thank you

Sturla is almost correct. What you really want is this:

i = [[i1], [i2], [i3]]
j = [[j1, j2, j3]]
B = A[i, j]

http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer

If you have more numpy questions, please ask on the numpy mailing list.

http://www.scipy.org/Mailing_Lists

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
an underlying truth."
-- Umberto Eco

Robert Kern, Sep 9, 2009
5. ### Robert KernGuest

On 2009-09-08 22:03 PM, sturlamolden wrote:
> On 9 Sep, 03:45, hi_roger<> wrote:
>
>> i know how to select a submatrix using the slice object in numpy. But
>> how can i select a submatrix
>> A[i1,i2,i3;j1,j2,j3] (elements in A on line i1,i2,i3 and column
>> j1,j2,j3 , and i1,i2,i3,j1,j2,j3 are all arbitrary numbers )

>
> You just pass an array of ints for each dimension. If you want a 3x3
> submatrix, you must pass in two 3x3 arrays of indices.

Oops! I just responded to the OP saying that you were "almost correct"; but I
was just thinking of your latter 1D example rather than this correct statement.
My apologies.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma