Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Is there an alternative to "find" for non-integer values?

Asked by Brian on 7 May 2013

Hi,

I need to find all the coordinates in a nx2 matrix which have a certain (x) value. All the coordinates are non-integer so find will not work.

Example:

 M = 2.334 3.554
     4.443 4.554
     3.245 5.332
     4.443 2.443

Is there a way to put [4.443 4.445; 4.443 2.443] into a new matrix? Can num2string and back again work or is there a better way? Thanks in advance

0 Comments

Brian

Products

No products are associated with this question.

1 Answer

Answer by Image Analyst on 7 May 2013
Edited by Image Analyst on 7 May 2013
Accepted answer

Check if it's within a tolerance, as recommended by the FAQ. Try this:

targetValue = 4.443;
tolerance = 0.01;
M = [2.334 3.554
     4.443 4.554
     3.245 5.332
     4.443 2.443]
   diffM = abs(M(:, 1) - targetValue)
   % Find rows within tolerance of the target value in row 1.
   rowsToExtract = diffM < tolerance
   % Extract only those rows:
   extractedRows = M(rowsToExtract, :)

In the command window you'll see:

M =
                   2.334                     3.554
                   4.443                     4.554
                   3.245                     5.332
                   4.443                     2.443
diffM =
                   2.109
                       0
                   1.198
                       0
rowsToExtract =
   0
   1
   0
   1
extractedRows =
                   4.443                     4.554
                   4.443                     2.443

1 Comment

Brian on 7 May 2013

That's brilliant, thanks very much

Image Analyst

Contact us