[討論] 改變matrix element without loop
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://www.ptt.cc/bbs/MATLAB/M.1431188378.A.744.html
→
05/10 00:53, , 1F
05/10 00:53, 1F
謝謝建議,但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
推
05/10 01:25, , 2F
05/10 01:25, 2F
※ 編輯: 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
MATLAB 近期熱門文章
PTT數位生活區 即時熱門文章