Search code examples
matlabmatrixmatrix-indexing

Indexing of 2D array in matlab


I have a 6X4 matrix M1 containing only zeros. I also have two 1D arrays Y1 and Y2 each with length 4.The two arrays contain the desired index values. Now, I want to set(convert to 1) the elements of matrix M1 such that M1(Y1:Y2) is equal to 1

for ex: Y1=[1 2 2 1] and Y2=[3 4 5 3]
then, M1 should be

 1 0 0 1
 1 1 1 1 
 1 1 1 1
 0 1 1 0
 0 0 1 0
 0 0 0 0

I can do this using for loop. But is there any optimised way to do it? (I intend to use much bigger matrices)


Solution

  • There may be other techniques, but this uses element wise operations which are insanely parallel.

    A very simple solution. Thanks @Shai

    >> [rows, cols] = size(M);
    >> Y1=[1 2 2 1]; Y2=[3 4 5 3]; 
    >> M = bsxfun(@ge, (1:rows)', Y1) & bsxfun(@le, (1:rows)', Y2)
    M =
         1     0     0     1
         1     1     1     1
         1     1     1     1
         0     1     1     0
         0     0     1     0
         0     0     0     0
    

    Unnecessarily complicated code

    [rows, cols] = size(M);
    offsets = ((1 : cols) - 1) * rows
    Y1 = offsets + Y1;
    Y2 = offsets + Y2;
    
    M = reshape(1:numel(M), rows, cols);
    M = bsxfun(@ge, M, Y1) & bsxfun(@le, M, Y2);