performancematlabmatrixvectorization

Change the matrix values using index vector


I have the following array:

AA = zeros(5,3);
AA(1,3)=1;
AA(3,3)=1;
AA(4,2)=1;

and I want to place the value one in the columns defined by the following vector

a = [0; 2; 0; 0; 1]

Each value of this vector refers to the column index that we want to change in each row. When zero appears, no changes should be made.

Desired output:

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

Could you please suggest a way to do this without for loop? The goal is a faster execution.

Thanks!


Solution

  • Approach 1

    nrows = size(AA,1) %// Get the no. of rows, as we would use this parameter later on
    
    %// Calculate the linear indices with `a` as the column indices and 
    %// [1:nrows] as the row indices
    idx = (a-1)*nrows+[1:nrows]'  %//' 
    
    %// Select the valid linear indices (ones that have the corresponding a as non-zeros
    %// and use them to index into AA and set those as 1's
    AA(idx(a~=0))=1
    

    Code output with given AA -

    >> AA
    AA =
         0     0     1
         0     1     0
         0     0     1
         0     1     0
         1     0     0
    

    Approach 2

    AA(sub2ind(size(AA),find(a~=0),a(a~=0)))=1
    

    Breaking it down to few steps for explanation: