Failing to create matrix from cell array in several ways
4 views (last 30 days)
Show older comments
לק"י
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 Comments
Stephen23
on 20 Aug 2024
Edited: Stephen23
on 20 Aug 2024
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.
Answers (1)
Paul
on 19 Aug 2024
Edited: Paul
on 19 Aug 2024
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 Comments
Stephen23
on 20 Aug 2024
Edited: Stephen23
on 20 Aug 2024
"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')
See Also
Categories
Find more on Matrix Indexing 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!