how to resize a matrix two pair of two numbers stacked together?

2 ビュー (過去 30 日間)
MP
MP 2022 年 6 月 22 日
コメント済み: MP 2022 年 6 月 23 日
i have a matrix like a = 1:16; %1x16 double to
I want a new matrix having
b = [1 2 9 10; 3 4 11 12; 5 6 13 14; 7 8 15 16]'; % 4x4 double
Two numbered pair to be rized to 4 by 4 matrix.
Can anyone please help. Any help is greatly appriciated.

採用された回答

Abhijit Nayak
Abhijit Nayak 2022 年 6 月 22 日
According to my understanding you want to arrange the odd and the even numbers from a 1-D matrix into a 2-D matrix but with a certain arrangement.
I have given a code according to your given number list but it can be modified according to the requirement.
a = 1:16;
b=zeros(4,4);
a_odd=zeros(1,16/2);
a_even=zeros(1,16/2);
x=1; y=1;
for i=1:16
if rem(a(i),2)==0
a_even(1,x)=a(i);
x=x+1;
else a_odd(1,y)=a(i);
y=y+1;
end
end
x=1; y=1;
for i=1:4
for j=1:4
if rem(i,2)==0
b(i,j)=a_even(x);
x=x+1;
else
b(i,j)=a_odd(y);
y=y+1;
end
end
end
disp(b);
  1 件のコメント
MP
MP 2022 年 6 月 23 日
that is what i wanted. thank you so much.....

サインインしてコメントする。

その他の回答 (1 件)

Karim
Karim 2022 年 6 月 22 日
you can do this with the reshape command:
a = (1:16)'
a = 16×1
1 2 3 4 5 6 7 8 9 10
b = reshape(a, 4, [] )
b = 4×4
1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by