find function with floating points number

조회 수: 1 (최근 30일)
sermet
sermet 2014년 5월 8일
답변: Star Strider 2014년 5월 8일
distances=[100.2545 120.6632;200.8588 220.4465]
points_id=[{'p1'} {'p2'};{'p3'} {'p4'}]
pointfind = @(d) points_id(find(distances == d));
p = pointfind(120.6632)
%it gives
p1 =
Empty cell array: 0-by-1
%this function only works with integers but I need to work with floating points number.

채택된 답변

Star Strider
Star Strider 2014년 5월 8일
This revision of pointfind should work:
pointfind = @(d) points_id(find(fix(distances*10^(length(num2str(rem(d,1)))-2))/(10^(length(num2str(rem(d,1)))-2)) == d));
(It obviously all goes on one line, although it wraps here.)
You are dealing with differences in precision between the values stored in distances and the precision in the argument ‘d’ you present to pointfind. The revised version of pointfind truncates the elements of distances to the precision of ‘d’ before it does the comparison. It works with decimal fractions now.

추가 답변 (1개)

Alessandro
Alessandro 2014년 5월 8일
  댓글 수: 1
sermet
sermet 2014년 5월 8일
that's no the same thing I'm looking for. Here I need to define pointfind function as working floating numbers.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by