Reputation: 73
Say I have a vector:
A = [1.444, 1.425, 1.435, 1.438, 1.438, 1.436, 1.436, 1.436, 1.436, 1.436];
As can be seen, this vector A
stabilises or converges at 1.436
. How can I find the index of this value for example 1.436
in MATLAB?
Edit:
More Examples:
B = [1 2 1 4 2 5 6 2 𝟱 5 5 5 5 5 5 5 5 5 5]
C = [224.424 224.455 224.454 224.456 224.456 224.452 224.451 224.456 𝟮𝟮𝟰.𝟰𝟱𝟰 224.454 224.454 224.454 224.454 224.454 224.454]
So the output I want is the index for when the elements in the vectors doesn't change anymore. Say for example that the values in the vectors are taken at a time t
. So for the first vector that index would be at index 9
, when the elements stay constant at 5
.
Same thing with vector C
. The wanted output here is index 9
, when the elements are constant at 224.454
.
Upvotes: 0
Views: 265
Reputation: 1556
According to your edit, assume that the vector will always converge and the converged value is the last element (A(end)
). Also, assume that when converged, the values are equal to the last element.
The idea is to first find the index of the last element that is not equal to the last element. Then, the index + 1 is the index of the first converged element, i.e. find(A~=A(end),1,'last') + 1
Example 1:
A = [1.444, 1.425, 1.435, 1.438, 1.438, 1.436, 1.436, 1.436, 1.436, 1.436];
index = find(A~=A(end),1,'last') + 1
Output:
index =
6
Example 2
B = [1 2 1 4 2 5 6 2 5 5 5 5 5 5 5 5 5 5 5];
index = find(B~=B(end),1,'last') + 1
Output:
index =
9
Example 3
C = [224.424 224.455 224.454 224.456 224.456 224.452 224.451 224.456 224.454 224.454 224.454 224.454 224.454 224.454 224.454];
index = find(C~=C(end),1,'last') + 1
Output:
index =
9
Update:
Since you are dealing with convergence, it is better to specify the tolerance for convergence. For example:
tolerance = 1e-5;
A = [1.444, 1.425, 1.435, 1.438, 1.438, 1.436, 1.436, 1.436, 1.436, 1.436];
index = find(abs(A - A(end)) >= tolerance,1,'last') + 1
Output:
index =
6
Upvotes: 2