필터 지우기
필터 지우기

cell to matrix conversion with a slight modification

조회 수: 2 (최근 30일)
hrushikesh kyathari
hrushikesh kyathari 2019년 7월 30일
댓글: Guillaume 2019년 7월 31일
I have a 1×501 cell array(named C). Each element in the cell is a 4*4 matrix, which means I have 501 matrices of 4*4 dimensions. What I have to do is: for each matrix in the cell, all the elements in the particular matrix must be put in 1 row.
for example I have to convert
[1,2,3,4 [0,1,0,0
5,6,7,8 1,0,0,0 ..........so one upto 501 matrices
9,10,11,12 0,0,1,1
13,14,15,16] 0,0,0,1]
into
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
0,1,0,0,1,0,0,0,0,0,1,1,0,0,0,1
.............
........
so on upto 501 rows. ]
Help if possible.

채택된 답변

Guillaume
Guillaume 2019년 7월 30일
Assuming that you mean a 501x16 matrix, then:
m = reshape(cat(3, yourcellarray{:}), [], numel(yourcellarray)).'
This concatenate the 501 matrices into a 4x4x501 matirx array, reshape that into a 16x501 matrix where each column is a flattened matrix and then transpose into a 501x16 matrix.
  댓글 수: 6
hrushikesh kyathari
hrushikesh kyathari 2019년 7월 31일
no, I meant the last row of each matrix, so if a matrix is
[1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16]
I need it to be made as [1,2,3,5,6,7,9,10,11,4,8,12]. So I want to apply this to all the matrices
So this would result in final outcome as 501*12 martix.
Guillaume
Guillaume 2019년 7월 31일
Well, then simply remove the last 4 columns from the output
croppedm = m(:, 1:end-4);
or you can do:
m = cat(3, yourcellarray{:})
m = reshape(permute(m(1:end-1, :, :), [3, 2, 1]), numel(yourcellarray), [])

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by