How to change matrix numbers positions

I have a matrix generated randomly and I want to change the positions of the numbers
i.e:
X=[2 3 4 5;
7 8 9 1]
to
X=[9 5 1 3;
5 2 8 2]
(I'm the one who chooses the new positions, not random)

Respuestas (3)

Georgios Pyrgiotakis
Georgios Pyrgiotakis el 30 de Nov. de 2018

0 votos

do you mean that you want to randomly reanrage the array?
Image Analyst
Image Analyst el 30 de Nov. de 2018
Will this work for you?
X = [2 3 4 5;
7 8 9 1]
[rows, columns] = size(X)
X = X(randperm(numel(X))) % Unfortunately reshapes into a row vector.
X = reshape(X, [rows, columns]) % Put back into 2-D
It rearranges elements into a random order.

2 comentarios

Hugo Matias
Hugo Matias el 30 de Nov. de 2018
No, i'm the one who chooses the new positions!
Image Analyst
Image Analyst el 30 de Nov. de 2018
What form do you have your new positions in? A matrix? A vector? Two vectors or two matrices with new rows in one, and new columns in the other? Come on, make it EASY for us to help you, not hard.

Iniciar sesión para comentar.

Bruno Luong
Bruno Luong el 30 de Nov. de 2018
X(randperm(numel(X))) = X

4 comentarios

Hugo Matias
Hugo Matias el 30 de Nov. de 2018
i'm the one who chooses the new positions!
Bruno Luong
Bruno Luong el 30 de Nov. de 2018
Editada: Bruno Luong el 30 de Nov. de 2018
Even easier:
X(:) = X(TheNewPositionYouChoose)
Hugo Matias
Hugo Matias el 30 de Nov. de 2018
How would I do if I wanted to to change the position of the number at (1,2) to (2,3) , for example?
Bruno Luong
Bruno Luong el 30 de Nov. de 2018
Put 3 in NewPositionYouChoose(6)

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 30 de Nov. de 2018

Comentada:

el 30 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by