Matlab函數accumarray的用法

來源:互聯網
上載者:User

碰到accumarray

這個函數是在Steve的文章[1
]中。

 

這是一個很靈活的build-in函數,所以是沒有m檔案,看不到實現代碼的。

 

開始理解起來比較拗,看了看例子知道什麼意思了。

 

Matlab的Help

[2
]裡這麼說:

accumarray

groups elements
from a data set and applies a function to each group. A = accumarray(subs,val)

creates an array A

by
accumulating elements of the vector val

using the
elements of subs

as indices. The position of an
element in subs

determines which value of vals

it
selects for the accumulated vector; the value of an element in subs

determines
the position of the accumulated vector in the output.

對於A = accumarray(subs,val)這麼一個調用,有這麼幾個問題,理解清楚,就理解了這個函數。

例子:

val    =     [     1     2     3     4     5    ]

subs =     [     1     2     4     2     4    ]'  % subs要是列向量  

Q: accumarray總體是幹嘛的?

A: 籠統的說,是用subs向量中的資訊從val中提取數值做累加,累加完的結果放到A中。

 

Q: subs是幹嘛的?

A: subs是一個累加指示向量。

subs提供的資訊由兩個:

(a). subs向量中的每個位置對應val的每個位置;

(b). subs中元素值相同的,val中的對應元素累加,元素值是累加完後放到A的什麼地方。

       如:上面的例子中,subs(2),subs(4)都是2,所以,val(2)和val(4)累加起來,放到A(2)這個位置上。

 

Q: val是幹嘛的?

A: val是提供累加數值的,誰累加呢?就是A中的數值累加。選哪些數進行累加呢?subs向量中數值相同的對應位置的數。累加完後放到哪裡呢?放到subs中指示的位置。

 

Q: A是怎麼出來的?A的維度是什嗎?A的內容如何確定?

A: A的維度是subs中表示維度數值最大的那個,如例子中size(A,1)==4,因為max(subs)==4。當然,這隻是一維的情況。

最後A的結果就是:

A =<br /> 1 % subs(1)==1,所以,A(1) = val(1)。<br /> 6 % subs(2)==subs(4)==2,所以,A(2)=val(2)+val(4)<br /> 0 % subs中沒有數值是3的值,也就是說A(3)上不累加任何數值<br /> % val(3)對應的subs(3)==4,所以val(3)累加到A(4)上去了<br /> 8 % subs(3)==subs(5)==4,所以,A(4)=val(3)+val(5)

 

Steve中用accumarray實現了一個從座標組兒中統計位置的功能:

pairs = [...<br /> 1 3; 1 2;<br /> 2 1; 2 4;<br /> 3 1; 3 4;<br /> 4 2; 4 3;<br /> 5 6; 5 7;<br /> 6 5; 6 8;<br /> 7 5; 7 8; 7 9;<br /> 8 6; 8 7; 8 10;<br /> 9 7; 9 10;<br /> 10 8; 10 9]<br />A = accumarray(pairs, 1)<br />結果:<br />A =<br /> 0 1 1 0 0 0 0 0 0 0<br /> 1 0 0 1 0 0 0 0 0 0<br /> 1 0 0 1 0 0 0 0 0 0<br /> 0 1 1 0 0 0 0 0 0 0<br /> 0 0 0 0 0 1 1 0 0 0<br /> 0 0 0 0 1 0 0 1 0 0<br /> 0 0 0 0 1 0 0 1 1 0<br /> 0 0 0 0 0 1 1 0 0 1<br /> 0 0 0 0 0 0 1 0 0 1<br /> 0 0 0 0 0 0 0 1 1 0

 

這是accumarray一種靈活的用法。

 

可以非常高效地用統計一個矩陣中不重複的數值有哪些,見rocwoods的文章
[3
]。

 

太晚了,簡單寫一下,還有很多細節問題沒有列出來:二維甚至更高維的應用,除了sum還可以自己定義函數處理等等,知道大概意思之後,可以仔細看Matlab的Help[2
]了。

References:

[1] Connected component labeling - Part 3 | Steve on Image Processing,http://blogs.mathworks.com/steve/2007/03/20/connected-component-labeling-part-3/


[2] accumarray - The MathWorks,http://www.mathworks.com/help/techdoc/ref/accumarray
.html



[3] 向量化操作的又一重要函數accumarray的用法總結,http://www.simwe.com/forum/thread-811616-1-3.html

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.