看板 MATLAB 關於我們 聯絡資訊
目前有一亂數矩陣 x=round(rand(100,2).*10) 假設前五項是 1 1 1 1 1 1 1 1 2 2 經過運算會出現一新矩陣 1 1 4(1 1矩陣出現4次) 2 2 1 請問一下這種該如何下手呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.58.176.226 ※ 文章網址: https://www.ptt.cc/bbs/MATLAB/M.1438856981.A.6AB.html
celestialgod: unique(x, 'rows')在每一行做迴圈計算相等的列數 08/06 19:36
OuOcc: 使用此函數不就把相同向量剔除了嗎? 請問該如何計算次數呢? 08/06 20:04
celestialgod: m=unique(x, 'rows'); count=zeros(size(m, 1), 1); 08/06 20:14
celestialgod: for i=1:size(m, 1), count(i)=sum(all(x=m(i, :), 08/06 20:16
celestialgod: 1));end; 08/06 20:16
OuOcc: 非常感謝。我後來搭配accumarray寫出來了 感恩S 08/06 20:30
at01076172: 啊!這篇文章太晚出現了XD 雖然看著下面的回應又多學 08/09 14:32
at01076172: 到了新的東西 08/09 14:32
at01076172: 我的方法是 [C,~,ic]=unique(x,'rows'); count=histc( 08/09 14:33
at01076172: ic,unique(ic)); 08/09 14:33
at01076172: 除了histc 也可以嘗試使用 count=sum(bsxfun(@eq,ic,u 08/09 14:34
at01076172: nique(ic)')); 08/09 14:34
celestialgod: 原po應該是用accumarray(ic, ones(size(x,1),1)) 08/09 14:42
celestialgod: 後來搜尋到這個 http://tinyurl.com/o99zwe7 08/09 14:48
celestialgod: 我覺得第一個方法很酷 08/09 14:49
at01076172: 哦哦!話說我當初好像也有查到tabulate,但它只吃向量, 08/09 15:47
at01076172: 所以後來想著想著才想起ic才解決這個只吃向量的問題 08/09 15:47
at01076172: 目前小弟還剛開始使用matlab一個月而已,還需要跟大家 08/09 15:48
at01076172: 多多學習:D 08/09 15:48