cell to matrix conversion with a slight modification

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 日

1 投票

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 日
編集済み: hrushikesh kyathari 2019 年 7 月 31 日
Thanks for the reply, but I guess I didn't explain my question properly which led to miscomprehension. I editted it, could you please look into it again.
Guillaume
Guillaume 2019 年 7 月 31 日
編集済み: Guillaume 2019 年 7 月 31 日
Your edit is exactly what my code does except that since matlab is column major, the matrix
[1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16]
gets reshaped into
[1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16]
If the order does really matter then you ought to work with columns rather than rows as that would avoid a lot of expensive (in term of time) transpositions / permutations, but this will do:
m = reshape(permute(cat(3, yourcellarray{:}), [3, 2, 1]), numel(yourcellarray), [])
hrushikesh kyathari
hrushikesh kyathari 2019 年 7 月 31 日
Thanks a lot, I actually did a silly mistake as I copied your 1st answer but gave the output as my original cell .My bad, sorry for that.
The order does matter for me, I need the order to be as [1,2,3,5,6,7,9,10,11,4,8,12]
I actually don't need the last row. I would be pleased if you could help me here.
Guillaume
Guillaume 2019 年 7 月 31 日
If by last row, you mean the last cell of the cell array, then just delete it before the conversion, or:
m = reshape(permute(cat(3, yourcellarray{1:end-1}), [3, 2, 1]), numel(yourcellarray)-1, [])
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 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by