www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/AccumulateDataExample.m

    %% Accumulate Data  

%% 
% Create a vector of data, |val|, and a vector of subscript values with
% the same length, |subs|. 
val = 101:105';
subs = [1; 3; 4; 3; 4]  

%% 
% Use |accumarray| to sum the values in |val| that have identical subscripts
% in |subs|. 
A = accumarray(subs,val) 

%%
% The result is a vector of accumulated values. Since the second and fourth
% elements of |subs| are equal to 3, |A(3)| is the sum of the second and
% fourth elements of |val|, that is, |A(3) = 102 + 104 = 206|. Also, |A(2)
% = 0| because |subs| does not contain the value |2|. Since |subs| is a
% vector, the output, |A|, is also a vector. The length of |A| is |max(subs,[],1)|.