How to vectorize this for loop

2 次查看(过去 30 天)
Master Blabla
Master Blabla 2020-11-19
How to vectorize this for loop
%%% F is array of float
%%% SD is two dimension array of float
for i=1:10
F(i) = SD(i,:);
end

回答(1 个)

Walter Roberson
Walter Roberson 2020-11-19
Fea = Fin( sub2ind(size(Fin), SD(1:10,:), 20*ones(10, size(SD,2))));
However, your original code would have failed. Your SD is 2d, so SD(i,:) would have been a vector of values, and indexing FD() with a vector of row indices and one column index would return a vector of values rather than a single value -- and so you would not be able to store it into the scalar location Fea(i) .
The only way for your code to work would be if SD were a column vector instead of a 2D array.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by