randomly shuffle the matrix by taking two rows

For example, if I have
1 5 9 13 17 20
2 6 10 14 18 21
3 7 11 15 19 22
4 8 12 16 20 23
I want to have a function that can shuffle the columns around while keeping the values in the column the same. I want it done by taking two columns together.For example:
9 13 17 20 1 5
10 14 18 21 2 6
11 15 19 22 3 7
12 16 20 23 4 8
In other words, the first two columns were taken together, the next two columns were taken together and randomly mixed in that way.
(Note : My matrix is normally a very large matrix. That's why I wanted to do it with code)
Can you help me to code this?
Thank you.

 Respuesta aceptada

A = [1 5 9 13 17 20
2 6 10 14 18 21
3 7 11 15 19 22
4 8 12 16 20 23];
new_idx = randperm(size(A,2)/2)*2-[1; 0]
new_idx = 2×3
5 3 1 6 4 2
A = A(:,new_idx(:)) % A(:,[5 6 3 4 1 2])
A = 4×6
17 20 9 13 1 5 18 21 10 14 2 6 19 22 11 15 3 7 20 23 12 16 4 8

4 comentarios

I actually didn't want that.
I have a matrix like this:
1 9 8 5 3 9
3 7 7 4 1 10
4 10 6 2 4 9
4 9 7 1 3 5
I want to mix it randomly. But I want it in twos.
Example :
8 5 3 9 1 9
7 4 1 10 3 7
6 2 4 9 4 10
7 1 3 5 4 9
I tried your codes but it didn't give the result I wanted.
It does exactly what you are saying:
A = [1 9 8 5 3 9
3 7 7 4 1 10
4 10 6 2 4 9
4 9 7 1 3 5];
new_idx = randperm(size(A,2)/2)*2-[1; 0];
A = A(:,new_idx(:))
A = 4×6
8 5 3 9 1 9 7 4 1 10 3 7 6 2 4 9 4 10 7 1 3 5 4 9
Yes this is exactly what i want. Thank you very much.
Voss
Voss el 21 de Mzo. de 2022
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Deep Learning Toolbox en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 21 de Mzo. de 2022

Comentada:

el 21 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by