Interchange dimensions of cell array and the matrices included in it

조회 수: 1 (최근 30일)
Ahmad Gad
Ahmad Gad 2021년 8월 19일
편집: Turlough Hughes 2021년 8월 20일
Hello.
Assume that I have a cell array as shown below:
Can I convert it to the following form:
I need to interchange the number of columns in the cell with the number of columns in the matrices inside. I was able to do it in few lines. But is there a direct way to make this conversion?
Thanks a lot,
Ahmad Gad

답변 (1개)

Turlough Hughes
Turlough Hughes 2021년 8월 19일
편집: Turlough Hughes 2021년 8월 20일
Here's one way
temp = vertcat(originalData{:});
D = repmat({zeros(3,27)},1,1000); % preallocate
for ii = 1:1000
D{ii} = reshape(temp(:,ii),3,27);
end
The approach takes the first column from each matrix in the 1x27 input cell array and concatenates the them making a 3x27 matrix for the first cell of the output; then continues to the second input columns for the second cell in the output, and so on and so forth.
EDIT
You could use splitapply as follows:
D = splitapply(@(x) {reshape(x,3,27)}, vertcat(originalData{:}), 1:1000);
Actually, much simpler is not orgainsing the data into cells in the first place - you can have a 3 by 27 by 1000 matrix and this is just easier to work on in the long run:
D = reshape(vertcat(originalData{:}),3,27,1000);
  댓글 수: 3
Ahmad Gad
Ahmad Gad 2021년 8월 19일
Hello,
I will check this. I acutally don't look for effeciency in the current application.
Thanks a lot
Ahmad
Turlough Hughes
Turlough Hughes 2021년 8월 19일
Ok I've added in a one-liner (see edit above). Other than that, I doubt there are more direct functions for what you want to do.

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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by