作者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/m.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