Change dimension of matrices in a cell variable

2 visualizzazioni (ultimi 30 giorni)
JINGYU KANG
JINGYU KANG il 15 Mag 2020
Risposto: Vedant Shah il 9 Apr 2025
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

Risposte (1)

Vedant Shah
Vedant Shah il 9 Apr 2025
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"))

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by