Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
MP
MP am 22 Jun. 2022
Kommentiert: MP am 23 Jun. 2022
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.

Akzeptierte Antwort

Abhijit Nayak
Abhijit Nayak am 22 Jun. 2022
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);

Weitere Antworten (1)

Karim
Karim am 22 Jun. 2022
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

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by