Discussion:
Vectorization question
(too old to reply)
Debashis
2012-09-12 04:58:59 UTC
Permalink
Hi, I have a problem with vectorization in Octave. I have a M x N matrix A (M rows). I have another vector v of size M, each of this contains a row index for A (between 1 to N).
For every row, I want to assign a constant value to the corresponding column element.
The equivalent code using for loop is:
for i = 1:size(A, 1)
A(i, v(i, 1)) = c;
end;

What is the best way to vectorize this?

Thx.
b***@gmail.com
2012-12-19 00:20:11 UTC
Permalink
Try with:

indx = sub2ind(size(A), 1:size(A, 1), v);
A(indx) = c;

Loading...