[討論] 改變matrix element without loop

看板MATLAB作者 (橋南荀令過,十里送衣香)時間10年前 (2015/05/10 00:19), 10年前編輯推噓1(101)
留言2則, 2人參與, 最新討論串1/1
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
我猜 A(1:20, B(:))=1應該可以
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
sub2ind
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
文章代碼(AID): #1LJZEQT4 (MATLAB)
文章代碼(AID): #1LJZEQT4 (MATLAB)