How to delete all rows of a matrix wherever any value is invalid (999)?

8 ビュー (過去 30 日間)
Lu Da Silva
Lu Da Silva 2021 年 11 月 11 日
回答済み: Steven Lord 2021 年 11 月 11 日
I have a matrix M of 100 rows and 4 colums.
Whenever column 1 of M is 99 I want to delete the entire row. Also whenever column 4 of M is 999 I want to delete the entire row.
I tried idx = any((M(:,1)==99),2); M(idx,:) = [];
but it did not work

回答 (3 件)

KSSV
KSSV 2021 年 11 月 11 日
idx = any(M==999,2) ;
M(idx,:) = [] ;

Awais Saeed
Awais Saeed 2021 年 11 月 11 日
M = [99 1 25 999; 10 1 2 6; 14 99 25 65; 99 1 2 9; 9 1 999 10]
[row1,~] = find(M(:,1) == 99) % search for 99 in col1
[row2,~] = find(M(:,4) == 999)% search for 999 in col4
del_row = unique([row1;row2]) % remove duplicate row numbers
M(del_row,:) = [] % delete those rows

Steven Lord
Steven Lord 2021 年 11 月 11 日
Let's make a sample matrix M with some 999 values present in columns 1 and 4. I'll also make a backup copy of it so we can modify the original several times, restoring from the backup each time.
M = randi(10, 10, 5);
M([2 7], 1) = 999;
M([3 7], 4) = 999;
backupM = M;
disp(M)
7 6 2 6 10 999 2 9 8 7 10 5 4 999 6 10 2 10 7 4 4 10 4 10 9 5 9 4 3 10 999 9 4 999 2 7 10 5 10 5 9 2 7 9 10 4 2 4 10 4
Now let's approach this in three different ways. The first creates variables for each column to check (though this could be combined into one command, replacing column1Has999 and column4Has999 with the right-hand side expression in the line of code that creates the variable eitherColumnHas999.
column1Has999 = M(:, 1) == 999 % should indicate rows 2 and 7
column1Has999 = 10×1 logical array
0 1 0 0 0 0 1 0 0 0
column4Has999 = M(:, 4) == 999 % should indicate rows 3 and 7
column4Has999 = 10×1 logical array
0 0 1 0 0 0 1 0 0 0
eitherColumnHas999 = column1Has999 | column4Has999 % should indicate rows 2, 3, and 7
eitherColumnHas999 = 10×1 logical array
0 1 1 0 0 0 1 0 0 0
M(eitherColumnHas999, :) = [] % rows 2, 3, and 7 have been removed
M = 7×5
7 6 2 6 10 10 2 10 7 4 4 10 4 10 9 5 9 4 3 10 7 10 5 10 5 9 2 7 9 10 4 2 4 10 4
But if you want to check a large collection of columns that's going to be a lot of repetitive code. Instead you could use any.
M = backupM; % restore from backup copy
columnsToCheck = [1 4];
checkedColumnsHave999 = any(M(:, columnsToCheck) == 999, 2) % rows 2, 3, and 7
checkedColumnsHave999 = 10×1 logical array
0 1 1 0 0 0 1 0 0 0
M(checkedColumnsHave999, :) = []
M = 7×5
7 6 2 6 10 10 2 10 7 4 4 10 4 10 9 5 9 4 3 10 7 10 5 10 5 9 2 7 9 10 4 2 4 10 4
A third option would be to standardize the representation of missing data in M using standardizeMissing and then to use rmmissing.
M = backupM; % restore from backup
M = standardizeMissing(M, 999) % Convert 999 to the "standard" missing for double, NaN
M = 10×5
7 6 2 6 10 NaN 2 9 8 7 10 5 4 NaN 6 10 2 10 7 4 4 10 4 10 9 5 9 4 3 10 NaN 9 4 NaN 2 7 10 5 10 5 9 2 7 9 10 4 2 4 10 4
M = rmmissing(M, 1) % Eliminate rows that contain a missing value
M = 7×5
7 6 2 6 10 10 2 10 7 4 4 10 4 10 9 5 9 4 3 10 7 10 5 10 5 9 2 7 9 10 4 2 4 10 4

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by