Is there a way to do the following without running a for loop.
1 次查看(过去 30 天)
显示 更早的评论
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!