Search code examples
arraysmatlabindexingdimensionscells

Create a 1-D array that shows the indexes of a 2-D array


Lets say, we have the following two-dimensional array in Matlab:

A=[0 451
   0 446
   0 543
   .....]

etc. I want to create another, one-dimensional array, that will do this: For example, lets call the 1-D array B, B(1) will "show" to [0 451]. B(2) will "show" to [0 446], B(3) will "show" to [0 543] and so on.I hope that my desired result is pretty clear to anyone who could give me a bit help.


Solution

  • Two ways:

    a=1:10
    split_a1=(reshape(a,2,[])).';
    

    Access split_a1 as split_a1(1,:),...,split_a1(5,:);.

    split_a2=mat2cell(a,1,2*ones(1,numel(a)/2));
    

    Access split_a2 as split_a2{1},...,split_a2{5};.