Convert cell array to set of matrices

조회 수: 1 (최근 30일)
Studentskp
Studentskp 2022년 6월 10일
댓글: Studentskp 2022년 6월 13일
I am trying to convert a cell aray into a 30 set of matrices orsmaller arrays of size 100 x 2. I need to sort these matrices (using 'sortrows' and get the mean of each row later).
Cell array (say C) size is 1 x 30, each cell is 100 x 2
I am trying to use a for loop for this.
for ii =1:30
M(ii) =C{ii};
end
I get an error message saying "indices on left side are not compatible with the size of right side". Any ideas about how to solve this?
Thanks,
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 6월 10일
Why do you want to do this? Why not just leave them as cell array?
sC = cellfun(@sortrows, C, 'UniformOutput', 0);
mr = cellfun(@(M) mean(M, 2), sC, 'UniformOutput', 0);
Stephen23
Stephen23 2022년 6월 10일
편집: Stephen23 2022년 6월 10일
So you are trying to create lots of dynamically-named variables in the workspace, thus forcing yourself into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug:
"Any ideas about how to solve this? "
Use indexing and your cell array. Indexing is neat, simple, and very efficient (unlike what you are trying to do).
"I need to sort these matrices (using 'sortrows' and get the mean of each row later)."
You already have a loop and use indexing: what is stopping you from sorting the matrices inside that loop?

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

답변 (1개)

Studentskp
Studentskp 2022년 6월 12일
Thanks very much for the replies, I think I should explain the question a bit better.
In my 1 x 30 cell array, each cell is 100 x 2. Each cell contain 100 observations recorded at certain time intervals, (say, T(E)).
But the observations are recorded at different time instances. One set of observations (e.g. cell 1,2 3,7 13, etc) starts at 0.1 ms and T(E) is 0.001 ms. The other observations statrts 1 ms and T(E) is 0.002 ms.
I want to bring them into one array, without changing the actual time of observation. So, my array will have one 'time' column and 30 columns of obervations. Length of the columns will not be 10 anymore.
The way i am trying to do this is, create 30 matrices (each matrix 100 x 2), and use 'sortrpws'. The I can take mean and std of each row.
I am new to MATLAB so not sure if this is the bst way.
Thanks.
  댓글 수: 4
Walter Roberson
Walter Roberson 2022년 6월 13일
To confirm: you want to create an array in which every time used in any cell is in the time vector, and for each file if that exact time appears then the value should be filled in, but any cell that does not have that exact time should have nan?
Studentskp
Studentskp 2022년 6월 13일
yes, that's correct.

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by