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

댓글 수: 3

David Hill
David Hill 2020년 11월 19일
You need to provide the Fin() function. With a loop of only 10, why do you want to vectorize?
Star Strider
Star Strider 2020년 11월 19일
Master Blabla
Master Blabla 2020년 11월 19일
편집: Master Blabla 2020년 11월 19일
F

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 11월 19일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

태그

질문:

2020년 11월 19일

편집:

2020년 11월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by