Clear Filters
Clear Filters

put each column of a matrix into different cells

12 views (last 30 days)
I have a matrix of size (4x4)
a = [1 2 3 4; 123 4 5 6; 52 5 4 7; 2 5 8 7];
b = mat2cell(a, ?)
I want b to be a {4x1} or {1x4} cell where every cell contains the column values of matrix
b = cell{1,:} = [1; 123; 52; 2] , cell{2,:} = [2 ; 4 ; 5 ; 5] and so on. but i am not being able to figure out mat2cell input parameters

Accepted Answer

Mehmed Saad
Mehmed Saad on 11 May 2020
Edited: Mehmed Saad on 11 May 2020
Use num2cell. it is easy
a = [1 2 3 4; 123 4 5 6; 52 5 4 7; 2 5 8 7];
b = num2cell(a,1)
b =
1×4 cell array
{4×1 double} {4×1 double} {4×1 double} {4×1 double}

mat2cell

is usefulll for other purpose
b = mat2cell(a.',[1 1 1 1])
b =
1×4 cell array
{4×1 double} {4×1 double} {4×1 double} {4×1 double}

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!