Failing to create matrix from cell array in several ways
Afficher commentaires plus anciens
לק"י
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 commentaires
Seems to work; Which version of Matlab are you using? Is cell content of analysisdayaWT only a single value or are they arrays as well?
x = num2cell(rand(64,64,21)); % random cell array
x(8,2,1)
x_new = cell2mat(x(8:15, 2:60, 1:21));
x_new(1,1,1)
"Error message I get"
The error is due to the content of the cell array. Clearly the content have sizes that cannot be concatenated together, for example if some of them are scalar and some are non-scalar (e.g. empty or non-scalar arrays).
If you want further help with this then upload a sample cell array in a MAT file, by clicking the paperclip button.
"When I try something else"
The second syntax does not work because you generate a comma-separated list when you use curly-braces:
Amit Ifrach
le 20 Août 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.
Amit Ifrach
le 20 Août 2024
Réponses (1)
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 commentaires
Amit Ifrach
le 20 Août 2024
Déplacé(e) : Stephen23
le 20 Août 2024
Amit Ifrach
le 20 Août 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')
Catégories
En savoir plus sur Data Type Conversion dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
