For loop to delete rows from matrix
2 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
Jan
2023 年 2 月 13 日
編集済み: Jan
2023 年 2 月 14 日
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 件のコメント
Voss
2023 年 2 月 13 日
P = M(M(:, 2) == 3, :); % Keep only rows, which have a 3 in 2nd column
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!