필터 지우기
필터 지우기

For loop to delete rows from matrix

조회 수: 1 (최근 30일)
Dimitri Haan
Dimitri Haan 2023년 2월 13일
편집: Jan 2023년 2월 14일
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.

답변 (1개)

Jan
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
Dimitri Haan
Dimitri Haan 2023년 2월 13일
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 2023년 2월 13일
P = M(M(:, 2) == 3, :); % Keep only rows, which have a 3 in 2nd column

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by