Sort random number at a matrix

2 Ansichten (letzte 30 Tage)
Guilherme Lopes de Campos
Guilherme Lopes de Campos am 26 Sep. 2023
Beantwortet: Dyuman Joshi am 26 Sep. 2023
Dear community,
I would like to sort a matrix size (4998x2) on random form.
The follow code it was used:
n=4998
Matrixw = repmat(x, 1, ceil(n/numel(x)));
Matrixw = Matrixw(randperm(n));
The head of data is:
TT Test1
TT Test2
TT Test3
TT Test4
CN Test5
CN Test6
CN Test7
CN Test8
CN Test9
CN Test10
SN Test11
SN Test12
SN Test13
SN Test14
SN Test15
SN Test16
SN Test17
Using the code above, just first column is random sort, but, I need that respectivly both column of the matrix are random sort, in the other words:
SN Test17
TT Test4
CN Test7
Each pair of variable is random sort, but together.
Can help me, please?
Yours faithfully

Akzeptierte Antwort

Voss
Voss am 26 Sep. 2023
% a matrix
M = reshape(1:100,[],2)
M = 50×2
1 51 2 52 3 53 4 54 5 55 6 56 7 57 8 58 9 59 10 60
% randomly reorder the rows
M = M(randperm(size(M,1)),:)
M = 50×2
3 53 33 83 31 81 42 92 18 68 27 77 37 87 29 79 24 74 16 66

Weitere Antworten (1)

Dyuman Joshi
Dyuman Joshi am 26 Sep. 2023
Do you mean like this -
n = 10;
x = [(1:10)' (10:-1:1)']
x = 10×2
1 10 2 9 3 8 4 7 5 6 6 5 7 4 8 3 9 2 10 1
idx = randperm(n)
idx = 1×10
1 6 3 5 4 9 8 10 2 7
x = x(idx,:)
x = 10×2
1 10 6 5 3 8 5 6 4 7 9 2 8 3 10 1 2 9 7 4

Kategorien

Mehr zu Shifting and Sorting 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