Mohamed Farrag
Mohamed Farrag

Reputation: 1692

Select from 2D Array by 2 criteria - Matlab

I have a 15 * 2 array where the first column represents the area and the second column represents the corresponding circularity to the 15 objects. I need to select the row with maximum area while applying the following condition for the circularity to be > 0.9 and <= 1.2

Example:

Area      Circularity
-----     -----------
22041     1.1703
23458     2.8425
155       1.4165
37        2.1089
215       1.5692
41        1.0549
659       1.7144
64        1.0508
3         0.3092
584       1.2543
26        1.1132
396       2.9046
1         0
3         0.8488
4         0.4638

Expected Result:

22041    1.1703

Upvotes: 2

Views: 106

Answers (1)

Suever
Suever

Reputation: 65450

You can apply your conditional to the second column to check if it is in the range (0.9 1.2] and then multiply the resulting logical array with the first column. Since false will be treated as 0 and true will be treated as 1, this will zero-out the values in the first column that don't meet the criteria for the second column. You can then use the second output of max to get the row that contains the maximum value

[~, ind] = max(data(:,1) .* (data(:,2) > 0.9 & data(:,2) <= 1.2));
result = data(ind,:)

Upvotes: 3

Related Questions