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

 채택된 답변

Matt J
Matt J 2015년 8월 27일
편집: Matt J 2015년 8월 27일

3 개 추천

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

댓글 수: 3

Hi Matt, I've been trying your ndSparse class, but I'm not sure what the proper way of use is.
In my case I want to use a 3 dimensional sparse, of which two dimensions define a geometric object, the 3rd dimension is for time.
Beforehand I know the size of the 3 dimensions, and an estimate of the number of non-zero elements, so I can use spalloc to pre-allocate the ndSparse object:
AllResults = ndSparse.spalloc([numel(x_vector),numel(y_vector),numel(time)],NNZestimate);
Next, I use normal indexing of the AllResults object as I would do with a 3d-vector, to assign the results of each time step. Before I do that, I place the results in a sparse:
for t = 1:numel(time)
....somecalculations...
AllResults(:,:,t) = sparse(xindices,yindices,aboveresults,numel(x_vector),numel(y_vector));
end
Is this the correct way to work with ndSparse objects?
Matt J
Matt J 2018년 12월 6일
편집: Matt J 2018년 12월 6일
You can do that, but the more efficient way is to use ndSparse.build,
T=numel(time);
X=numel(x_vector);
Y=numel(y_vector);
coordinates=cell(T,1);
values=cell(T,1);
for t = 1:T
....somecalculations...
coordinates{t}=[xindices(:), yindices(:), xindices(:)*0+t];
values{t}=aboveresults(:);
end
coordinates=cell2mat(coordinates);
values=cell2mat(values);
AllResults = ndSparse.build(coordinates,values,[X,Y,T],NNZestimate);
Lucademicus
Lucademicus 2018년 12월 7일
편집: Lucademicus 2018년 12월 7일
Thank you for the suggestion, it greatly reduced the computational time!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2015년 8월 27일

1 개 추천

No, sparse matrices are limited to 2 dimensions. If you feel strongly that MATLAB should extend sparse matrices to 3 or more dimensions, please send your use case (how you would use those higher dimensional sparse arrays) to Technical Support and ask them to include that information in the enhancement database.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

UTA
2015년 8월 27일

편집:

2018년 12월 7일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by