Using cell array to create 3D sparse matrix.
3 次查看(过去 30 天)
显示 更早的评论
Collet Bastien
2012-10-2
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
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 个评论
Matt J
2012-10-5
编辑:Matt J
2012-10-5
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
2012-10-5
编辑:Matt J
2012-10-5
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
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.
Collet Bastien
2012-10-9
编辑:Collet Bastien
2012-10-9
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
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
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
2012-10-9
Here are the values when the execution fails :
data =
All zero sparse: 81-by-30
E =
type: '()'
subs: {':' [1]}
rhs =
(1,1) 0.3333
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
Ok, can you send me an e-mail (by pm) address that I can use to send you the data.
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
Ok,I'm looking for a new matlab version, I'm also going to send you the variables too.
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
