Remove rows in a matrix where there isn't a change larger than 3 in either column

1 回表示 (過去 30 日間)
Michael Sposato
Michael Sposato 2023 年 4 月 6 日
コメント済み: Image Analyst 2023 年 4 月 6 日
I have a 225x2 matrix where the first 200 rows or so (it varies by data set) does not change by more than 3 in either column. I need a function to delete all of the rows where there isn't a change greater than 3 and then condense the matrix.

回答 (1 件)

Image Analyst
Image Analyst 2023 年 4 月 6 日
Do you mean a change between the first column and second column in each row?
Try this
changes = m(:, 2) - m(:, 1); % Change from col 1 to col 2.
rowsToDelete = changes > 3;
m = m(~rowsToDelete, :);
  3 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 4 月 6 日
In the example you mentioned, both columns in the 2nd row don't change by more than 3 from the 1st row.
Now, do you want to remove the 2nd row, and compare the 3rd row to the first? Or do you want to compare the 3rd row to the 2nd one?
Image Analyst
Image Analyst 2023 年 4 月 6 日
You forgot to give the output. Is the first row to be deleted too? Or just the second and third row?
m = [...
1000 520
1000 521
1001 519
800 250
324 104];
changes = diff(m, 1) % Change from one row to the next
changes = 4×2
0 1 1 -2 -201 -269 -476 -146
rowsToDelete = [0; any(changes >= 0 & changes < 3, 2)]
rowsToDelete = 5×1
0 1 1 0 0
m = m(~rowsToDelete, :)
m = 3×2
1000 520 800 250 324 104

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by