Failing to create matrix from cell array in several ways
4 次查看(过去 30 天)
显示 更早的评论
לק"י
Hi guys,
I have a cell array in 64X61X21 size.
I want to make a matrix of the values (no vectors) within specific area of the cell array - 15:21,2:61,1:21.
First I make a copy of the cell array:
cellarrayneeded=analysisdayaWT(8:15, 2:60, 1:21);
cellarrayneeded=cell2mat(cellarrayneeded);
Error message I get:
Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in cell2mat (line 118)
ct{mref{:}} = cat(cdim+1,c{mref{:},:});
When I try something else:
cellarrayneeded=cell2mat(analysisdataWT{8:15, 2:60, 1:21});
I get:
Error using cell2mat
Too many input arguments.
What am I doing wrong?
Thanks!
5 个评论
Stephen23
2024-8-20
编辑:Stephen23
2024-8-20
It is very easy to confirm that not all of the data in that cell array are scalar:
S = load('analysisdataWTcopy.mat')
C = S.analysisdataWTcopy;
X = cellfun(@isscalar,C);
all(X(:)) % nope, not all scalar.
nnz(X) % how many are scalar?
nnz(~X) % how many are non-scalar?
all(cellfun(@isempty,C(~X))) % the non-scalar arrays are all empty
Is the range correct? Are the cell array contents correctly defined or imported? We don't know, you need to debug.
Actually looking at your data is much more reliable than relying on what you believe your data to be like.
回答(1 个)
Paul
2024-8-19
编辑:Paul
2024-8-19
Hi Amit,
To subscript into a cell array and return a cell array, use () as with normal array indexing, which should have worked in your first case. We can't see what's contained in your analysisdataWT variable, but it looks like the elements are not all scalars.
% create example cell array
x = rand(64,61,21);
analysisdataWT = num2cell(x);
% pull out subarray and convert to numeric matrix
y = cell2mat(analysisdataWT(15:21,2:61,1:21));
% check
isequal(y,x(15:21,2:61,1:21))
3 个评论
Stephen23
2024-8-20
编辑:Stephen23
2024-8-20
"when I try your mehtod I get this error: Dimensions of arrays being concatenated are not consistent."
This answer does not state that it will fix your incompatibly-sized data.
"... the range I chose contains only scalars."
And yet ... there is always an error with your data. Who do you think is right: you (relying on untested assumptions and beliefs about data) or MATLAB (which uses your actual data)? Once you upload your data then we can help you. And tell us the output from this command:
all(cellfun(@isscalar,analysisdayaWT(8:15, 2:60, 1:21)),'all')
另请参阅
类别
在 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!