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

2 visualizaciones (últimos 30 días)
MP
MP el 22 de Jun. de 2022
Comentada: MP el 23 de Jun. de 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.

Respuesta aceptada

Abhijit Nayak
Abhijit Nayak el 22 de Jun. de 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);

Más respuestas (1)

Karim
Karim el 22 de Jun. de 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

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by