JackWM
JackWM

Reputation: 10565

Find specific rows with specific conditions

E.g. I have the following matrix Data with 3 columns: time sat and usr

1234 1  1
3134 0  10
34123 0 9
2342 1  10
232 1  1
...

Given selectedusr=[1 10], how to select rows in which usr is contained in selectedusr?

To get SelectedData:

1234 1  1
3134 0  10
2342 1  10
232 1  1
...

Upvotes: 0

Views: 60

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112769

Use ismember to select the desired rows:

SelectedData = Data(ismember(Data(:,3),selectedusr),:);

Upvotes: 2

A. Donda
A. Donda

Reputation: 8476

SelectedData = Data(any(bsxfun(@eq, Data(:, 3), selectedusr(:)'), 2), :);

bsxfun(@eq, ...) compares every element of the usr column with every element of selectedusr and results in a logical array with one column for each element of selectedusr. any(..., 2) determines rows of this matrix where at least one the logical values is true, resulting in a logical column vector. This is used to select the corresponding rows from Data via logical indexing.

Upvotes: 1

Related Questions