Failing to create matrix from cell array in several ways

לק"י
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

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)
ans = 1x1 cell array
{[0.9891]}
x_new = cell2mat(x(8:15, 2:60, 1:21));
x_new(1,1,1)
ans = 1x1 cell array
{[0.9891]}
Stephen23
Stephen23 2024년 8월 19일
편집: Stephen23 2024년 8월 19일
"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:
לק"י
Hi stephen, thanks for the answer. I'm adding copy of he extracted data.
About the tutorial, I'll read it aright away.
thanks again,
Amit.
It is very easy to confirm that not all of the data in that cell array are scalar:
S = load('analysisdataWTcopy.mat')
S = struct with fields:
analysisdataWTcopy: {7x60x21 cell}
C = S.analysisdataWTcopy;
X = cellfun(@isscalar,C);
all(X(:)) % nope, not all scalar.
ans = logical
0
nnz(X) % how many are scalar?
ans = 8232
nnz(~X) % how many are non-scalar?
ans = 588
all(cellfun(@isempty,C(~X))) % the non-scalar arrays are all empty
ans = logical
1
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.
לק"י
Hi stephen, thanks!
mmm something indeed is wierd. i'll look it up. it shouldn't be like that.
Thanks!

댓글을 달려면 로그인하십시오.

답변 (1개)

Paul
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))
ans = logical
1

댓글 수: 3

Amit Ifrach
Amit Ifrach 2024년 8월 20일
이동: Stephen23 2024년 8월 20일
לק"י
Hi guys,
First of all thank you very much.
my cells contain single scalars as vectors. I add an image of the array. pay attention that other cells within the cell array are cell arraysm but the range I chose contains only scalars.
thanks!
לק"י
Hi, when I try your mehtod I get this error:
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 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')

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2024년 8월 19일

댓글:

2024년 8월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by