Home > OS >  Matlab matrix to cell
Matlab matrix to cell

Time:05-12

I'm trying to avoid loops in Matlab. How can I do the following matrix to cell conversion vectorized?

m1 = ones(10, 2);
i = [1:10]';
m2 = [i i];
c = cell(10, 2);

for i=1:10
    c{i, 1} = m1(i, :);
    c{i, 2} = m2(i, :);
end

CodePudding user response:

As mentioned by @beaker mat2cell() is the function to use here...this should work:

c = mat2cell([m1,m2],ones(10,1),[2,2])

  • Related