How do I vectorize this for loop?

y_vec=zeros(100,10);
% y_vec is initially a zeros matrix. y is a vector of dimension 100 x 1 containing values between 1 and 10. For the value
% at ith row in vector y, y_vec(i,value)=1
for i=1:100
y_vec(i,y(i))=1;
end

回答 (1 件)

Rik
Rik 2020 年 6 月 4 日

0 投票

You will need to convert to linear indices with sub2ind.

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

質問済み:

2020 年 6 月 4 日

回答済み:

Rik
2020 年 6 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by