ColeS ColeS - 3 months ago 8
Python Question

Matlab to python logic difficulty for arrays

I have created an m-by-n matrix in MATLAB and can easily select a range of values within a certain column and row. For instance, if I have matrix

A
:

A =

0 0 0 0
1 2 3 4
5 6 7 8
9 10 11 12


I can isolate the values: 1,5 and 9 from the first column by typing:
A(2:4,1)
. The results will yield
[1;5;9]
. As it relates to python, I am not sure how to index an array such that I have the desired values as above.

Answer

This can be done using numpy

a = numpy.matrix('0 0 0 0; 1 2 3 4; 5 6 7 8; 9 10 11 12')

Required result is a[1:,0] or a[1:4,0]

Only difference is that the array indexing start from 0 instead of 1.

Comments