Using cell array to create 3D sparse matrix.

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.

回答(1 个)

Matt J
Matt J 2012-10-2
编辑:Matt J 2012-10-2
Consider using this FEX contribution instead
It will let you do many/most things that you can do with 3D full arrays.

17 个评论

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)."
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.
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.
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,
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
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,
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.
Here are the values when the execution fails :
data =
All zero sparse: 81-by-30
E =
type: '()'
subs: {':' [1]}
rhs =
(1,1) 0.3333
I need a program that I can run myself (ideally a short one), that will let me reproduce this error.
I sent you a pm with all the details.
I'm afraid I did not receive it.
Ok, can you send me an e-mail (by pm) address that I can use to send you the data.
You can use the contact link here
Also, tell me what MATLAB version you are using.
I'm using the version R2009b (I forgot to write it in the pm).
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.
Ok,I'm looking for a new matlab version, I'm also going to send you the variables too.
I received your file, but
PV(:,:,1)=AAA;
works fine for me. I'm betting that upgrading will fix it.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by