Is there a way to do the following without running a for loop.
1 回表示 (過去 30 日間)
古いコメントを表示
Srinivas Gopal Krishna
2020 年 7 月 30 日
コメント済み: Srinivas Gopal Krishna
2020 年 7 月 31 日
clear all
y=randi(5,1,5)';
A=zeros(5,5);
for i=1:5,
A(i,y(i))=1;
end
I want to know if this can be implemented without using a for loop.
0 件のコメント
採用された回答
Bruno Luong
2020 年 7 月 30 日
編集済み: Bruno Luong
2020 年 7 月 30 日
y = randi(5,1,5)'
Then
A = accumarray([(1:5)' y(:)], 1, [5 5]);
or
A = zeros(5,5);
A(sub2ind(size(A),1:5,y'))=1
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!