Azzera filtri
Azzera filtri

For loop to delete rows from matrix

1 visualizzazione (ultimi 30 giorni)
Dimitri Haan
Dimitri Haan il 13 Feb 2023
Modificato: Jan il 14 Feb 2023
I have a number of matrices that are thousands of rows long. I am trying to keep rows that have a certain number in the second column, and delete everything else. I am trying to use a for loop with an embedded if statement but I don't know where to start. Any tips would be nice thank you.

Risposte (1)

Jan
Jan il 13 Feb 2023
Modificato: Jan il 14 Feb 2023
M = randi([0, 5], 10000, 5); % Some test data
P = M(M(:, 2) == 3, :); % Keep only rows, which have a 3 in 2nd column
% [EDITED] Typo fixed, ",:" inserted. Thanks, Voss.
  2 Commenti
Dimitri Haan
Dimitri Haan il 13 Feb 2023
This was very helpful but i was hoping to keep more rows of the matrix not just the one column. Basically it would be "if the value of column 2 equals 1, keep the entire row. If the value of column 2 doesnt equal 1, delete the entire row
Voss
Voss il 13 Feb 2023
P = M(M(:, 2) == 3, :); % Keep only rows, which have a 3 in 2nd column

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by