Index matrices and columns in MATLAB, is the code ok?

I have some MATLAB code that I have to translate to Python to work with it. Going through the code I found this line:

filtered_proj (new_len, :, 1) = 0;

      

So after reading the MATLAB Documentation here , I figured out and tried to apply my Python code. I created a matrix in Python using NumPy for validation:

array([[1, 2],
       [3, 4],
       [5, 6]])

      

but when i try to access as written in MATLAB with the following python code

a[1,:,1]

      

I am getting this message:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
IndexError: too many indices for array

      

If I access as a[1: ,1]

, I get this:

array([4, 6])

      

And how a[1,: 1]

, I get the following:

array([3])

      

So, is this MATLAB code really correct? If so, how should I do this?

+3


source to share


3 answers


The MATLAB index indicates that you have a 3D array. Your numpy array is a 2D matrix of 3 rows, 2 columns. You need to create a 3D array for your test to work. Also, keep in mind that numpy indexes start at zero and MATLAB indexes start at 1.

The index a[1:, 1]

gets the second element of each column in all rows starting from the second. So, [4, 6]

this is exactly the expected result.

Likewise, a[1, :1]

selects the second row before (but not including) the second column. Since the string has two elements, it returns correctly [3]

.

For a 3D index like a[1, :, 1]

or to work a[0, :, 0]

, initialize your test array as a 3D array:



a = np.array([[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]])

      

or better yet:

a = np.arange(1, 13).reshape(2, 3, 2)

      

Now a[0, :, 0]

gives [1, 3, 5]

and a[1, :, 1]

gives [8, 10, 12]

as expected.

+2


source


Without additional context in the MATLAB code, I cannot comment if the MATLAB code is correct.

But it myMatrix = numpy.array([[1, 2], [3, 4], [5, 6]])

creates a 3 x 2 matrix where there are 3 rows of length 2. These three rows are [1,2], [3,4], [5,6] as your input says. The outer parenthesis around it all tells numpy that you are creating a matrix.



The syntax a[1,:,1]

fails because there are only 2 dimensions in the array you created, not 3. These dimensions are the "dimension" of the row and the dimension of the column.

+2


source


First check the size of the variable filtered_proj

in Matlab workspace (i.e. run ndims(filtered_proj)

). If it is 3d then other answers apply, but if it is a 2d matrix I have an answer for you.


Given a variable with size n, Matlab is flexible about indexing with more than n dimensions , provided the additional indices are 1.

For example,

X = 5;
display(X(1,1,1,1,1));

      

5 will be displayed

But

X = 5;
display(X(1,1,1,1,5));

      

Will display the error "Index exceeds the size of the matrix".

In numpy code, you declare a 2d matrix. In Matlab, you can index into a 2d matrix with a(1,:,1)

, because the final index is 1. However, Numpy is stricter and generates an error.

So when you translate code from Matlab to numpy, you have to remove the extra dimension from the command, i.e. a[1,:]

...

+2


source







All Articles