Change dimension of matrices in a cell variable

1 回表示 (過去 30 日間)
JINGYU KANG
JINGYU KANG 2020 年 5 月 15 日
回答済み: Vedant Shah 2025 年 4 月 9 日
Dear MATLAB community, I have made a cell whose dimension is a*b*c, which contains matrices of dimension d*1.
I want to change the dimension of matrices in such cell so that I get a cell variable whose dimension is a*d, which contains matrices of dimension c*b
Can I do such process using mat2cell, cell2mat, or any other function without using for loop or with only few for loops?
CHANGE (d*1) matrices in (a*b*c) cell
INTO (c*b) matrices in (a*d) cell

回答 (1 件)

Vedant Shah
Vedant Shah 2025 年 4 月 9 日
To convert an existing a x b x c cell array containing ‘dx1’ matrices into an ’a x d’ cell array, you can follow these steps:
  1. Loop over each value of ‘a’
  2. For each iteration, convert the ‘b x c’ cell slice into a matrix using cell2mat.
  3. Reshape the matrix into the desired ‘d’ size.
  4. Convert it back into a ‘c x b’ cell array using mat2cell.
  5. Append the result to the rows of the new cell array.
Below is a code snippet for your reference:
newCell = cell(a, d);
for i = 1:a
matrices = cell2mat(reshape(originalCell(:, :), a, []));
reshapedMatrices = reshape(matrices, d, []);
splitMatrices = mat2cell(reshapedMatrices, ones(1, d), c*b);
newCell(i, :) = cellfun(@(x) reshape(x, c, b), splitMatrices, 'UniformOutput', false);
end
For more information, you can refer to the documentation using the following commands in the MATLAB command window:
web(fullfile(docroot, "/matlab/ref/mat2cell.html"))
web(fullfile(docroot, "/matlab/ref/cell2mat.html"))
web(fullfile(docroot, "/matlab/ref/reshape.html"))

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by