Azzera filtri
Azzera filtri

How to eliminate the junk values in the index

6 visualizzazioni (ultimi 30 giorni)
Ganesh Kini
Ganesh Kini il 27 Apr 2020
Commentato: Image Analyst il 27 Apr 2020
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

Risposte (1)

Steven Lord
Steven Lord il 27 Apr 2020
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 Commento
Image Analyst
Image Analyst il 27 Apr 2020
But is that the right eps? The value of eps depends on the range of the numbers you're considering:
>> eps
ans =
>> eps(1)
ans =
>> eps(100)
ans =
>> eps(900)
ans =
>> eps(mean(x))
ans =
So what is the right argument to pass in to eps()? Not sure, so maybe using ismembertol() makes things more certain.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by