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 个)
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.
Collet Bastien
2012-10-9
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
2012-10-9
编辑:Collet Bastien
2012-10-9
Matt J
2012-10-9
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
2012-10-9
编辑:Collet Bastien
2012-10-9
Collet Bastien
2012-10-9
Matt J
2012-10-9
I need a program that I can run myself (ideally a short one), that will let me reproduce this error.
Collet Bastien
2012-10-9
Matt J
2012-10-9
I'm afraid I did not receive it.
Collet Bastien
2012-10-9
You can use the contact link here
Also, tell me what MATLAB version you are using.
Collet Bastien
2012-10-10
Matt J
2012-10-10
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
2012-10-10
编辑:Collet Bastien
2012-10-10
Matt J
2012-10-10
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!