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

조회 수: 4 (최근 30일)
Sarah A
Sarah A 2019년 1월 19일
댓글: Huw S 2021년 12월 28일
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,

채택된 답변

Guillaume
Guillaume 2019년 1월 19일
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
Huw S
Huw S 2021년 12월 28일
Can't you get the same outcome with just B = num2cell(A) ?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephan
Stephan 2019년 1월 19일
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
Sarah A
Sarah A 2019년 1월 19일
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]
}
Stephan
Stephan 2019년 1월 19일
A= [ 1 2 3 4; 5 6 7 8; 9 10 11 12]
B = mat2cell(A,[1 1 1],[2 2])
celldisp(B)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by