How does mat2cell work?

31 ビュー (過去 30 日間)
Diego Hens
Diego Hens 2021 年 1 月 15 日
コメント済み: Diego Hens 2021 年 1 月 15 日
Hello,
I need your help understanding how cell2mat works. I have tried so hard, but I just don't get it.
I have:
  • a Matrix called Matrix_A with a size of 18x46092.
I would like to:
  • separate it so, that I get 15364 cells (this is the amount of points, 46092/3, for the x, y and z coordinates), each of these cells containing a 18x3 matrix (18 points each).
I have already done it with a line someone gave me. It is for the mean of Matrix_A. It works.
Matrix_A = transpose(mat2cell(mean(cell2mat(Matrix_A)), 1, 3*ones(size(Matrix_A,2),1)));
Can someone please explain how cell2mat works, so that I can use it myself? A solution (a line of code) solves the problem right now, but this is the second or third time I want to do this and can't. That's why I would prefere an easy to understand explanaition.
Thank you!
Edit: I cannot post the Matrix_A data as it is too big.

採用された回答

Steven Lord
Steven Lord 2021 年 1 月 15 日
Let's try this with a slightly smaller matrix, so you can see what's going on.
A = reshape(1:24, [3 8])
A = 3×8
1 4 7 10 13 16 19 22 2 5 8 11 14 17 20 23 3 6 9 12 15 18 21 24
Let's slice this into 3-by-2 chunks. We don't actually need to slice it row-wise (since we want each chunk to have 3 rows just like A has three rows) but we do need to slice it column-wise.
rowSliceSizes = 3; % Each cell has three rows like A does
columnSliceSizes = [2 2 2 2]; % Each cell contains 2 columns of A
Now slice.
C = mat2cell(A, rowSliceSizes, columnSliceSizes)
C = 1x4 cell array
{3×2 double} {3×2 double} {3×2 double} {3×2 double}
celldisp(C)
C{1} = 1 4 2 5 3 6 C{2} = 7 10 8 11 9 12 C{3} = 13 16 14 17 15 18 C{4} = 19 22 20 23 21 24
Now let's slice A into a 2-by-3 cell array. Each cell in the first row of the result should contain 2 rows of A and each cell in the second row should contain the third row of A. Each cell in a column of the result contains 3, 2, and 3 columns of A respectively:
D = mat2cell(A, [2 1], [3 2 3])
D = 2x3 cell array
{2×3 double} {2×2 double} {2×3 double} {1×3 double} {1×2 double} {1×3 double}
celldisp(D)
D{1,1} = 1 4 7 2 5 8 D{2,1} = 3 6 9 D{1,2} = 10 13 11 14 D{2,2} = 12 15 D{1,3} = 16 19 22 17 20 23 D{2,3} = 18 21 24
  1 件のコメント
Diego Hens
Diego Hens 2021 年 1 月 15 日
Thank you so much for your long and detailed answer. I have understood it and have applied it to my work.
Thank you for helping me to understand it and for not telling me the answer beforhand.
I am confident I will be able to correctly use this command next time.
Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by