find(V==ma​x(V),1,'fi​rst') returns index that is way beyond the first index where X reaches the maximum

7 views (last 30 days)
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
Torsten
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
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.

Sign in to comment.

Answers (1)

Steven Lord
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
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.

Sign in to comment.

Tags

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!