How to put each two elements in a matrix in a cell ?

Hello,
I want to create cell array using the elements of double array where each cell is a (1*2) matrix resulted by putting each two successive elements in a row in one cell. for example, suppose that we have the array A:
A= [ 1 2 3 4; 5 6 7 8; 9 10 11 12] and we want to create the cell array B where
B= { [1 2] [3 4] ;
[5 6] [ 7 8] ;
[9 10] [11 12] } . So any suggestion about how can I do that?
regards,

 Accepted Answer

Well, it's important to use proper formatting in your question. Stephan answered your original question according to the way it was formatted.
After your edit, it's still the same concept, use mat2cell. Read its documentation to learn how it works. In your case,
A= [ 1 2 3 4; 5 6 7 8; 9 10 11 12]
B = mat2cell(A, ones(1, size(A, 1)), ones(1, size(A, 2)/2) * 2)

1 Comment

Can't you get the same outcome with just B = num2cell(A) ?

Sign in to comment.

More Answers (1)

A= [ 1 2 3 4; 5 6 7 8; 9 10 11 12]
A=reshape(A',2,[])';
B = mat2cell(A,ones(1,size(A,1)))

4 Comments

Sarah A
Sarah A on 19 Jan 2019
Edited: Sarah A on 19 Jan 2019
Thank you. But how I can put the paris from the same row in seperate row ? I edited the matrix B in the question.
Simpler to use num2cell instead of mat2cell:
B = num2cell(reshape(A.', 2, []).', 2)
No I need B =
{
[1 2] [3 4]
[5 6] [7 8]
[9 10] [11 12]
}
This line of code giveme B=
{
[1 2]
[3 4]
[5 6]
[7 8]
[9 10]
[11 12]
}
A= [ 1 2 3 4; 5 6 7 8; 9 10 11 12]
B = mat2cell(A,[1 1 1],[2 2])
celldisp(B)

Sign in to comment.

Categories

Asked:

on 19 Jan 2019

Commented:

on 28 Dec 2021

Community Treasure Hunt

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

Start Hunting!