You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Using cell array to create 3D sparse matrix.
7 views (last 30 days)
Show older comments
Collet Bastien
on 2 Oct 2012
Hello, I would like to create a 3d-sparse matrix. Ofc, I know that matlab's toolbox doesn't have it so far.
Here is my code :
C=zeros(m^nun,m,nu);
I would like to use a cell array filled with 2d sparse matrix. Something like :
A = zeros(m^nun, 1);
B = cell(size(A));
SP = sparse(m, nu);
for j=1:m^nun
B{j} = SP;
end
Is it the right way to do it ?
Furthemore, can I use my matrix B in the same way as my matrix C ? For example, here is my code (and it works) :
C(1:x,A,1)=N;
But is it going to work with my matrix B as well ? (by doing : B(1:x,A,1)=N;)
Thank you, and sorry for the long post.
Answers (1)
Matt J
on 2 Oct 2012
Edited: Matt J
on 2 Oct 2012
Consider using this FEX contribution instead
It will let you do many/most things that you can do with 3D full arrays.
17 Comments
Matt J
on 5 Oct 2012
Edited: Matt J
on 5 Oct 2012
Collet Bastien commented:
"Thank you very much, this contribution is rather useful. However, I'm still having a problem on :
prod(AP(:,A(nl+i,:)~=0,i),2); (where AP is a ndsparse object)
it's because prod is not defined for the objects ndsparse (??? Undefined function or method 'prod' for input arguments of type 'ndSparse'.).
What can I do to fix this problem ? I didn't find a documentation for this toolbox (this toolbox is quiet big, maybe the function I'm looking for already exists)."
Matt J
on 5 Oct 2012
Edited: Matt J
on 5 Oct 2012
It looks like you are taking the product along a very small piece of AP, so as a workaround, you could simply do this:
prod(full(AP(:,A(nl+i,:)~=0,i)),2);
It's true that I didn't implement a PROD method for the class. I could put it on my TODO list if you really think it will be useful.
I didn't expect people to have much use for a PROD method because very often PROD(A,DIM) for a sparse array would give an array of all zeros. Also, even in cases where A is non-sparse along certain DIM, the dimensions of a sparse A are usually pretty large in practice, so PROD might often overflow in that case.
Collet Bastien
on 9 Oct 2012
The prod method for a sparse matrix is only useful in some special cases (as you said, the most of the time, we will get 0 or an overflow). Even if I need it, I don't think it's very interesting to add this method to the toolbox.
I'm in trouble with my 3d matrix, maybe can you help me. I have this matrix :
A = (1,1) 0.3333 and size : 27x3, class : ndSparse
PY = All zero sparse: 27-by-3-by-30 and size : 27x3x30, class : ndSparse
I'm trying to do : PY(:,:,1) = A and I've got the following error :
??? Error using ==> subsasgn Attempt to convert to unimplemented sparse type Error in ==> ndSparse>ndSparse.subsasgn at 671 data=builtin('subsasgn',data,E,rhs);
Maybe can you help me to figure out why this is happening.
Matt J
on 9 Oct 2012
Edited: Matt J
on 9 Oct 2012
Hmmm. Can you show me exactly what you're doing to create A and PY? And can you run the following code? When I do, I get no errors.
A=ndSparse.build([27,3]);
A(1,1)=1/3;
PY=ndSparse.build([27,3,30]);
PY(:,:,1)=A,
Collet Bastien
on 9 Oct 2012
Edited: Collet Bastien
on 9 Oct 2012
So :
PY is created by using ndSparse.build([27,3,30]);
A is created by doing :
E .* B .* C
where :
E : size = 27x3, class = double
B : size = 27x3, class = double
C : size = 27x3, class = ndSparse
Matt J
on 9 Oct 2012
Can you provide a short piece of code that reproduces the error (and the exact error message that you see)? Below is yet another code example that I used to imitate what you describe. I get no error when I run it,
E=rand(27,3);
B=rand(27,3);
C=ndSparse.sprand([27,3],.1);
A=E.*B.*C;
PY=ndSparse.build([27,3,30]);
PY(:,:,1)=A,
Collet Bastien
on 9 Oct 2012
Edited: Collet Bastien
on 9 Oct 2012
Here is the error :
??? Error using ==> subsasgn Attempt to convert to unimplemented sparse type
Error in ==> ndSparse>ndSparse.subsasgn at 671 data=builtin('subsasgn',data,E,rhs);
Error in ==> nBayes_ETU at 175 PY(:,:,i)= A; % A = 27x3 ndSparse
Maybe can I try to print the values in your function.
Collet Bastien
on 9 Oct 2012
Here are the values when the execution fails :
data =
All zero sparse: 81-by-30
E =
type: '()'
subs: {':' [1]}
rhs =
(1,1) 0.3333
Collet Bastien
on 9 Oct 2012
I sent you a pm with all the details.
Collet Bastien
on 9 Oct 2012
Ok, can you send me an e-mail (by pm) address that I can use to send you the data.
Collet Bastien
on 10 Oct 2012
I'm using the version R2009b (I forgot to write it in the pm).
Matt J
on 10 Oct 2012
I don't have anything that will extract .rar files, I'm afraid. Please make it easy for me. If you can't provide a short set of commands to reproduce the problem, please just save PY and A to a .mat file and post a .zip of that. Since they are both sparse, it should give a very small, easily downloadable .zip.
I'm starting to wonder if the problem is R2009b. Formally, only R2010b and higher are supported, though I didn't suspect R2009 would have any problems. Upgrading your MATLAB version might be the simplest solution.
Collet Bastien
on 10 Oct 2012
Edited: Collet Bastien
on 10 Oct 2012
Ok,I'm looking for a new matlab version, I'm also going to send you the variables too.
See Also
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)