how to resize a matrix two pair of two numbers stacked together?
1 回表示 (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
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 件)
Karim
2022 年 6 月 22 日
you can do this with the reshape command:
a = (1:16)'
b = reshape(a, 4, [] )
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!