How to arrange alternate cells of a column into two columns?

조회 수: 1 (최근 30일)
Manoj Kumar V
Manoj Kumar V 2023년 8월 16일
댓글: Dyuman Joshi 2023년 8월 16일
Let's say the column matrix is:
A =
2
6
7
3
8
3
2
9
7
5
4
1
And I need to obtain final result as
A =
2 6
7 3
8 3
2 9
7 5
4 1
Can I know the way to split in this way?

채택된 답변

Florian Bidaud
Florian Bidaud 2023년 8월 16일
편집: Florian Bidaud 2023년 8월 16일
A = [2 6 7 3 8 3 2 9 7 5 4 1]'
A = 12×1
2 6 7 3 8 3 2 9 7 5
B = [A(1:2:end) A(2:2:end)]
B = 6×2
2 6 7 3 8 3 2 9 7 5 4 1
  댓글 수: 3
Manoj Kumar V
Manoj Kumar V 2023년 8월 16일
How shall the matrix be formed when there are odd number of elements? Is there a way to make it even by putting zero for the last element? Lets say: A = [2 6 7 3 8 3 2 9 7 5 4]. How can I obtain
B = 6×2
2 6
7 3
8 3
2 9
7 5
4 0
Dyuman Joshi
Dyuman Joshi 2023년 8월 16일
That depends on the size you want to arrange -
%For a 2D array, define atleast one dimension
ncol = 2;
A = [2 6 7 3 8 3 2 9 7 5 4];
%Convert to column vector
A = A(:);
%Add required zeros
A = [A;zeros(1,rem(numel(A),ncol))];
B = reshape(A,ncol,[])'
B = 6×2
2 6 7 3 8 3 2 9 7 5 4 0

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

추가 답변 (0개)

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by