could anyone help me how to pair the numbers in the desired manner as shown below

A=1:16
R = reshape(A,4,[])
After executing the above command results in
R =
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Now I want to group the numbers in the desired manner
1 2 3 4 5 6 7 8
12 11 10 9 16 15 14 13
Could anyone please help me on this.

 Respuesta aceptada

This looks a quite a unique manner. Anyways, try this
A=1:16;
R = reshape(A,4,[]);
[R(:,1)' R(:,2)'; flip(R(:,3))' flip(R(:,4))']
ans = 2×8
1 2 3 4 5 6 7 8 12 11 10 9 16 15 14 13

3 comentarios

Thanks it works.
Could you please help me how to combine the following code of the same working principle
A=1:12
R = reshape(A,3,[])
R1=[R(:,1)' R(:,2)'; flip(R(:,3))' flip(R(:,4))']'
B=1:16;
R2 = reshape(B,4,[]);
R3=[R2(:,1)' R2(:,2)'; flip(R2(:,3))' flip(R2(:,4))']'
C=1:24
R4 = reshape(C,6,[])
R5=[R4(:,1)' R4(:,2)'; flip(R4(:,3))' flip(R4(:,4))']'
D=1:36
R6 = reshape(D,9,[])
R7=[R6(:,1)' R6(:,2)'; flip(R6(:,3))' flip(R6(:,4))']'
E=1:48
R8=reshape(E,12,[])
R9=[R8(:,1)' R8(:,2)'; flip(R8(:,3))' flip(R8(:,4))']'
In the above code A, B, C, D, E executes separately. But I need to combine all into a single code. Could you help me on this.
I am not sure what you mean by combining all into a single code, so here's my approach -
for i=[3 4 6 9 12]
z=1:4*i;
y=reshape(z,[],4);
R=[y(:,1)' y(:,2)'; flip(y(:,3))' flip(y(:,4))']
end
R = 2×6
1 2 3 4 5 6 9 8 7 12 11 10
R = 2×8
1 2 3 4 5 6 7 8 12 11 10 9 16 15 14 13
R = 2×12
1 2 3 4 5 6 7 8 9 10 11 12 18 17 16 15 14 13 24 23 22 21 20 19
R = 2×18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 27 26 25 24 23 22 21 20 19 36 35 34 33 32 31 30 29 28
R = 2×24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 36 35 34 33 32 31 30 29 28 27 26 25 48 47 46 45 44 43 42 41 40 39 38 37
Here R will be modified in each iteration. You can modify the code according to however you want to proceed.
Thanks for your help.

Iniciar sesión para comentar.

Más respuestas (1)

reshape(A, 8, 2)'

2 comentarios

This doesn't give the correct output
Whoops. Didn't pay attention to the order of the second row.
Then we need to use flip most likely.
A=1:16
R = reshape(A,4,[]);
R(:, 3) = flip(R(:, 3));
R(:, 4) = flip(R(:, 4));
ret = reshape(R, 8, 2)'

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 2 de Nov. de 2021

Comentada:

el 3 de Nov. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by