MATLAB: How to build a 3d sparse matrix

matlab function

Hi everyone: As we all know these has a sparse function in matlab, for example, there has two vector:
a = [1 2 3 1 5]; b=[1 2 4 1 6];
if I use spase function like this as follow:
sn = sparse(a,b,1);
It will shows the answer like this:
ans =
(1,1) 2
(2,2) 1
(3,4) 1
(5,6) 1
the third column means the count number of conresponding row and column in a and b. So my qusetion is if it is possible to build a 3d sparse matrix as the extend of the trandtional sparse function as follow: if there has two vector:
aa = [1 2 1 1 2 3]; bb=[1 2 1 1 2 4];cc=[1 2 1 1 2 5];
snn = sparse_3d(aa,bb,cc,1);
snn=
(1, 1, 1) 3
(2, 2, 2) 2
(3, 4, 5) 1
That is very nice of anyone can give me the answer. Thanks ahead! weihua

Best Answer

You could try my ndSparse class ( Download ),
>> snn = ndSparse.build([aa',bb',cc'],1)
snn(:,:,1) =
(1,1) 3
snn(:,:,2) =
(2,2) 2
snn(:,:,5) =
(3,4) 1
Related Question