Assignment and accessing using indices
9 次查看(过去 30 天)
显示 更早的评论
I had a question about how to do a particular type of assignment using indices. The code below is a simplified example.
k = zeros(4,4);
v = [2,3];
k(v,v) = 1;
My intention was to get a result that looks like
ans =
[0, 0, 0, 0;
0, 1, 0, 0;
0, 0, 1, 0;
0, 0, 0, 0]
But unfortunately the result is:
ans =
[0, 0, 0, 0;
0, 1, 1, 0;
0, 1, 1, 0;
0, 0, 0, 0]
So my question is: Is there a way to access individual elements (rather than rectangular regions) without using a for loop (Which I understand is computationally expensive in MatLab)?
0 个评论
采纳的回答
更多回答(2 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!