# How to randomly select 2 rows

1 visualizzazione (ultimi 30 giorni)
lech king il 5 Ott 2020
Modificato: lech king il 5 Ott 2020
Hi
I want to randomly select 2 rows of a matrix
The matrix may have an odd number of rows
How to randomly select these 2 rows
for example
Random numbers are inside the variable B
Select the rows accordingly with the numbers in the random variable
B=randperm(19)
No duplicate rows
In other words, if a row is selected, it will no longer be selected
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (2)

Ameer Hamza il 5 Ott 2020
Modificato: Ameer Hamza il 5 Ott 2020
You can use the output of randperm as index
M = rand(10, 5); % random matrix
rows = randperm(size(M,1), 2)
M_new = M(rows, :)
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Andreas Bernatzky il 5 Ott 2020
This would be my solution:
myMat = rand(10,50);
[nRow,~] = size(myMat);
randomRows = round((nRow - 1).*rand(2,1) + 1); % from 1 to nRow
firstRandomRow = myMat(randomRows(1),:);
secondRandomRow = myMat(randomRows(2),:);
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by