作者polymerase (桥南荀令过,十里送衣香)
看板MATLAB
标题[讨论] 改变matrix element without loop
时间Sun May 10 00:19:35 2015
A=20*10 matrix of zeros.
B=20*1 matrix of integers. Values are 1~10 integer
Let's say B=[3; 1; 2; 10 .....]
以下三行有没有办法vectorize? Many thanks.
for i=1:20
A(i,B(i))=1
end
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 73.171.14.119
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/MATLAB/M.1431188378.A.744.html
1F:→ celestialgod: 我猜 A(1:20, B(:))=1应该可以 05/10 00:53
谢谢建议,但try过了不行
>> A=zeros(2,3)
A =
0 0 0
0 0 0
>> A(:,[1 3])=1
A =
1 0 1
1 0 1
因为我是想要
1 0 0
0 0 1
2F:推 sunev: sub2ind 05/10 01:25
※ 编辑: polymerase (73.171.14.119), 05/10/2015 12:23:33
A=zeros(2,3);
B=[1 3];
C=[1 2; B];
ind=sub2ind(size(A),C(1,:), C(2,:))
% ind = 1 6
A(ind)=1
A =
1 0 0
0 0 1
sunev你是我的matlab神
膜拜
※ 编辑: polymerase (73.171.14.119), 05/10/2015 12:31:36