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 Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by