Invalid index error in Scilab when trying to access array element

Pieter picture Pieter · Nov 6, 2010 · Viewed 10.5k times · Source

I'm not sure why I can't do this in Scilab.

-->foo=zeros(500);

-->foo(300)
         !--error 21 
Invalid index.

Why do I get the 'Invalid index' error? I thought I had initialized foo as an array with 500 elements, each of which was set to 0?

Answer

Aditya Sengupta picture Aditya Sengupta · Nov 9, 2010

In Scilab, you have to give both the number of rows as well as the number of columns. So, if you want to create a 500x500 matrix, you need to say zeros(500, 500). If you want a 500x1 vector, you need to say zeros(500, 1).

If you want to create a zeros matrix that has precisely as many rows and columns as another matrix (say A), you need to say zeros(A). This is where the confusion stems from.

In Scilab, zeros(500) would take 500 as a 1x1 matrix and generate a zeros matrix of size 1x1, that is [0]. In MATLAB, zeros(500) would take 500 to be the size of the matrix required, assuming a square matrix.