I have a 2 dimensional array L
, and I am trying to create a vector of linear indices ind
for each row of this array.
L=
1 5 25 4 0 0
2 3 3 45 5 6
45 5 6 0 0 0
I am using lenr
to store the number of non zero elements in each row (starting from column 1).
lenr=
4
6
3
Then I have 1x45 array RULES
. Indices stored in L
refer to elements in RULES
. Since I want to vectorize the code, I decided to create linear indices and then run RULES(ind)
.
This works perfectly:
ind=sub2ind(size(L),1,lenr(1));
while this doesn't work:
ind=sub2ind(size(L),1:3,1:lenr(1:3));
Any ideas?
UPDATE:
This is what I initially tried to vectorize the code, but it did not works and that's why I checked linear indices:
rul=repmat(RULES,3);
result = rul((L(1:J,1:lenr(1:J))));
1:lenr(1:3)
instead oflenr(1:3)
? – excazaallind = reshape(1:numel(L),size(L,1),[])
and thenind = allind(L~=0)
. Not tested though. – Divakar