how to remove rows if only the first row contains a specified value

2 Ansichten (letzte 30 Tage)
Hey everyone,
How can I easily remove an entire row if only the first element of a row contains a certain value (thus only checking the first column).
I would like to use a function that only checks the first column for a value (e.g. 281) and if that value is found then subsequently deletes that row. in the example below that would result in D = [279;280, 6;7, 6;5]
[EDIT]: how can I include more than value? Is this possible with & or | ?
example:
A = [279; 280; 281];
B = [6; 7; 9];
C = [6; 5; 4];
D = [A B C];

Akzeptierte Antwort

Stephen23
Stephen23 am 26 Nov. 2018
Bearbeitet: Stephen23 am 26 Nov. 2018
One Value: the simple MATLAB way, either keeping the other rows:
>> X = D(:,1)~=281;
>> D = D(X,:)
D =
279 6 6
280 7 5
Or by removing those rows:
>> X = D(:,1)==281;
>> D(X,:) = []
D =
279 6 6
280 7 5
Multiple Values: use ismember, either keeping the other rows:
>> V = [281,279]; % values to be removed.
>> X = ismember(D(:,1),V);
>> D = D(~X,:)
D =
280 7 5
Or by removing those rows:
>> X = ismember(D(:,1),V);
>> D(X,:) = []
D =
280 7 5

Weitere Antworten (0)

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by