How to eliminate the junk values in the index

조회 수: 4 (최근 30일)
Ganesh Kini
Ganesh Kini 2020년 4월 27일
댓글: Image Analyst 2020년 4월 27일
a = (1,3,4,6,8,9,0,5,4,)
b = (3)
c = (4,56,7,8,5,1)
a, b, c are saved in .vec extension
a = load ('numbers.vec');
b = load ('numbers1.vec');
c = load ('numbers2.vec');
value = period_fun (2,1,1);
The above code is just for information
my actual isssue is here
idx = find (period_fun == 1.1); % suppose your are looking for 1.1 in period_fun
[a, b, c] = ind2sub (size (period_fun), idx);
I am trying to get the index of a, b, c
So when i run it. sometimes a carries more than one index
Suppose for eg for the code when i run it it get
a = 1,000 4,000
Expected answer is 1,000 but why did 4.0000 come?
How can we eliminate the other values?
Please help me out

답변 (1개)

Steven Lord
Steven Lord 2020년 4월 27일
x = [1 2 3 2 3 4 2];
locations = find(x == 2)
The output of this find call has three elements (2, 4, and 7) since x contains three instances of the number 2 at locations 2, 4, and 7. If you only want to find the first or last instances find accepts options to do this.
firstTwo = find(x == 2, 1, 'first') % Find the first 1 instances of 2
lastTwoTwos = find(x == 2, 2, 'last') % Find the last 2 instances of 2
As a side note, testing for exact equality with floating-point values like 1.1 could give you results you don't expect (see especially the first example in the common problems section on that page.) In that scenario you probably want to compare with a tolerance or perhaps use the ismembertol function.
x = 0:0.1:1;
find(x == 0.3) % Not found
find(abs(x-0.3) <= eps) % found element 4 in x
  댓글 수: 1
Image Analyst
Image Analyst 2020년 4월 27일
But is that the right eps? The value of eps depends on the range of the numbers you're considering:
>> eps
ans =
2.22044604925031e-16
>> eps(1)
ans =
2.22044604925031e-16
>> eps(100)
ans =
1.4210854715202e-14
>> eps(900)
ans =
1.13686837721616e-13
>> eps(mean(x))
ans =
1.11022302462516e-16
So what is the right argument to pass in to eps()? Not sure, so maybe using ismembertol() makes things more certain.

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

카테고리

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