1
votes

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 collumns defined by the following vector a = [0; 2; 0; 0; 1]. Each value of this vector refers to the collumn index that we want to change in each row. When zero apears 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!!!

1

1 Answers

2
votes

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:

  • find(a~=0) and a(a~=0) gets us the VALID row and columns indices respectively as needed for sub2ind(size(),row,column) format.

  • sub2ind gets us the linear indices, which we can use to index into input matrix AA and set those in AA as 1's.