필터 지우기
필터 지우기

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개)

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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by