**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# find(V==max(V),1,'first') returns index that is way beyond the first index where X reaches the maximum

3 views (last 30 days)

Show older comments

find(V==max(V),1,'first') returns index that is way beyond the first index where V reaches the maximum.

In the above picture, you can see that the curve reaches its maximum and plateaus. However, when I run find(X==max(X),1,'first') in the console, I get this:

Where's the error?

##### 16 Comments

Walter Roberson
on 4 Aug 2022

In the above picture, you can see that the curve reaches its maximum and plateaus.

As outside observers, we do not know that to be true. The location at 963 might be (for example) 10^-14 larger due to round off error or other calculation reasons.

Bruno Luong
on 4 Aug 2022

"In the above picture, you can see that the curve reaches its maximum and plateaus"

Yes I can see but what I see is not exact:

Compare numbers intsead of look at the graph (poor resolution) and data cursor (digit truncated)

Bruno Luong
on 4 Aug 2022

Edited: Bruno Luong
on 4 Aug 2022

Type this:

format long

I_B(963)-I_B(400)

I_B(963)-I_B(500)

I_B(963)-I_B(600)

I_B(963)-I_B(700)

I_B(963)-I_B(800)

Abdallah
on 4 Aug 2022

I see that there are different values, but... why? Also, is this why I'm getting this problem?

-------------

This is confusing... again, why is this happening?

Also, what am I supposed to do now? why is it so complicated to do such a simple task?

Torsten
on 4 Aug 2022

Edited: Torsten
on 4 Aug 2022

This is confusing... again, why is this happening?

The plateau values in I_B are not 100 % identical - they differ by a small amount. That's why the maximum value appears at a quite arbitrary index of the array. To test where I_B reaches the plateau, a test to return the index where the maximum and the actual value of I_B differ only by a small amount is appropriate. This is done by the command

find(max(I_B) - I_B < 1e-3,1)

It finds the first index where the maximum of I_B and an array element differ by less than 1e-3.

Abdallah
on 4 Aug 2022

Bruno Luong
on 4 Aug 2022

"I see that there are different values, but... why? Also, is this why I'm getting this problem?"

It's your data and all the difference are positive therefore

I_B(963) is slightly larger than all the rest.

Why? You should question the cause in the method/program that generates I_B, if that is not expected.

There is no issue in the find command that works correctly since I_B(963) is slightly larger than all the rest, as you have observed by yourself by comparing numbers.

Abdallah
on 4 Aug 2022

find(max(I_B) - I_B < 1e-3,1)

does the job.

After zooming in, I got:

Which is exactly what I was searching for.

As an extension to this question, can you tell me how to avoid this from happening in the future? is there a configuration to truncate values or maybe force the trapz function to generate truncated values?

Abdallah
on 4 Aug 2022

Edited: Abdallah
on 4 Aug 2022

@Bruno Luong, thanks for making it even clearer.

You should question the cause in the method/program that generates I_B, if that is not expected.

As I mentioned in the previous comment, how can I prevent my program from generating such problematic data? I_B was calculated using the trapz function.

Torsten
on 4 Aug 2022

Edited: Torsten
on 4 Aug 2022

As an extension to this question, can you tell me how to avoid this from happening in the future? is there a configuration to truncate values or maybe force the trapz function to generate truncated values?

Calculate everythin in full precision without rounding as before. Just keep in mind that numerically checking for equality of floating point numbers (in your case I_B == max(I_B)) doesn't make sense. Only checking for equality within a specified tolerance (in your case max(I_B) - I_B < eps) is reasonable.

Abdallah
on 4 Aug 2022

Calculate everythin in full precision without rounding as before

Was I rounding my calculations? I have no idea! Can you please tell me how to calculate in full precision without rounding? In my code, I didn't manipulate anything related to precision.... or maybe I should have?...

numerically checking for equality of floating point numbers doesn't make sense

But why? can't floating numbers be equal? why wouldn't they?

Torsten
on 4 Aug 2022

Was I rounding my calculations? I have no idea! Can you please tell me how to calculate in full precision without rounding? In my code, I didn't manipulate anything related to precision.... or maybe I should have?...

You asked "is there a configuration to truncate values or maybe force the trapz function to generate truncated values?" and with my answer I meant: Don't generate truncated values - use trapz as before without truncating or rounding.

But why? can't floating numbers be equal? why wouldn't they?

x1 = 0.33333333333333;

x2 = 1/3;

x1 == x2

ans = logical

0

Here is a discussion about the subject:

Stephen23
on 4 Aug 2022

"But why? can't floating numbers be equal?

Of course they can be equal. That does not mean that they will be equal.

"why wouldn't they?"

Because of how floating point error accumulates during any arithmetic using binary floating point numbers.

Read this and you will understand why: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

### Answers (1)

Steven Lord
on 4 Aug 2022

What are the sizes of the variables involved in creating your plot above, and what is the exact syntax for the plot function that you used in creating it?

If the X data for your plot was not the integers 1 through the number of values in the Y data then the index of the point with X coordinate 363 may be greater than 363. In the example below, index is not 1 but the value stored in the X array at that index is 1.

X = 0:0.25:2

X = 1×9

0 0.2500 0.5000 0.7500 1.0000 1.2500 1.5000 1.7500 2.0000

index = find(X == 1) % Will be > 1

index = 5

coordinate = X(index) % Will be 1

coordinate = 1

##### 1 Comment

Abdallah
on 4 Aug 2022

What are the sizes of the variables involved in creating your plot above

what is the exact syntax for the plot function that you used in creating it

plot(linspace(1,1441-(T_B+1)+1,1441-(T_B+1)+1),I_B)

If the X data for your plot was not the integers 1 through the number of values in the Y data

....

As we can see, the X data are indeed the integers 1 through the number of values in the Y data, since I_B has 1341 column (as displayed above).

So ""then the index of the point with X coordinate 363 may be greater than 363"" isn't correct.

### See Also

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

### Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)