H

#### hi_roger

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

S

#### sturlamolden

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:
True

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

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

S

#### sturlamolden

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.

R

#### Robert Kern

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

R

#### Robert Kern

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