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

    %% Accumulate Values into Sparse Matrix
% Use repeated subscripts to accumulate values into a single sparse matrix
% that would otherwise require one or more loops.
%
% Create a column vector of data and two column vectors of subscripts. 

% Copyright 2015 The MathWorks, Inc.

i = [6 6 6 5 10 10 9 9]';
j = [1 1 1 2 3 3 10 10]';
v = [100 202 173 305 410 550 323 121]';

%%
% Visualize the subscripts and values side-by-side.
[i,j,v]

%%
% Use the |sparse| function to accumulate the values that have identical
% subscripts.
S = sparse(i,j,v)