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

2 vues (au cours des 30 derniers jours)
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];

Réponse acceptée

Stephen23
Stephen23 le 26 Nov 2018
Modifié(e) : Stephen23 le 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

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by