Re: [問題] 矩陣問題已回收

看板MATLAB作者 (神無月 孝臣)時間15年前 (2008/11/13 16:24), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串5/34 (看更多)
※ 引述《yenger ()》之銘言: : 假設今天有兩個矩陣 : a=[2 3 4 1 2 3] : b=5*6的零矩陣 : 我想把b的 : 第一個column random選兩個出來 變成1 : 第二個column random選三個出來 變成1 : 第三個column random選四個出來 變成1 : 第四個column random選一個出來 變成1 : 做到第六個column : 請問這樣要怎麼做呢? : 感激不盡~~ for i = 1 : size( b , 2 ) index = randperm( size( b , 1 ) ) ; b( index( 1 : a( i ) ) , i ) = 1 ; end 測試 b = zeros( 5 , 6 ) ; a = [ 2 3 4 1 2 3 ] ; 執行結果: b = 0 0 1 0 1 1 1 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 1 1 0 1 0 0 0 1 -- Deserves death! I daresay he does. Many that live deserve death. And some die that deserve life. Can you give that to them? Then be not too eager to deal out death in the name of justice, fearing for your own safty. Even the wise cannot see all ends. Gandalf to Frodo -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.120.25.235
文章代碼(AID): #196-HAZF (MATLAB)
討論串 (同標題文章)
文章代碼(AID): #196-HAZF (MATLAB)