timestep = 
How to Reshape a Matrix into a Cell
Show older comments
I have a 821x9 matrix, which corresponsed to 821 time steps with all 9 entries of a 3x3 rotation matrix, in the formatting as follows:
timestep 1, R11 R12 R13 R21 R22 R23 R31 R32 R33
timestep 2, R11 R12 R13 R21 R22 R23 R31 R32 R33
etc...
timestep 821, R11 R12 R13 R21 R22 R23 R31 R32 R33
I want to convert this to a cell, so that I can have each timestep corresond to a single 3x3 rotation matrix.
In the end I want 821 cell entries, each containing a 3x3 rotation matrix like this:
cell entry 1, R11 R12 R13
R21 R22 R23
R31 R32 R33
cell entry 2, R11 R12 R13
R21 R22 R23
R31 R32 R33
etc...
cell entre 821, R11 R12 R13
R21 R22 R23
R31 R32 R33
I have tried using num2cell but have not been successful! Please help me.
Answers (2)
% 821x9 matrix (filled with integers for illustration)
M = reshape(1:821*9,9,[]).'
% convert to the required cell array
C = num2cell(permute(reshape(M.',3,3,[]),[2 1 3]),[1 2]);
% check the first few entries
C{1}
C{2}
C{3}
Transpose the row vector, call reshape, then transpose that result —
syms R11 R12 R13 R21 R22 R23 R31 R32 R33
k = 1;
timestep(k,:) = [R11 R12 R13 R21 R22 R23 R31 R32 R33]
cell{k} = reshape(timestep(k,:).', 3, []).'
cell{k}
I did it in the Symbolic Math Toolbos here for effect. Use the same code with your data for each value of ‘timestep’.
Also, subscript the results rather than creating separate variables.
.
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!