0

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.

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
Lamar
  • 63
  • 2
  • 9
  • 3
    If want something more complicated that just the last element (i.e. `A(end)`) then no, we won't "get the picture". You have to clearly, mathematically define what you want to achieve. How are you measuring convergence? Will it always be at the end of the array? Is this part of an iterative process or literally just a value grab from an array? [Edit] your question to make it clearer. – Wolfie Dec 04 '18 at 15:01
  • If you had corresponding "x axis" values, you could look at the gradient of the curve and find when its absolute value falls below a certain threshold (to be determined). – am304 Dec 04 '18 at 15:14
  • That’s the worst convergence I have seen in a long time! I mean the first value is closer than the second and the third value is closer than the fourth and fifth values. Where do you have this from? – Nicky Mattsson Dec 04 '18 at 18:54
  • @NickyMattsson a very low temporal resolution sampled spring-damper system? – rinkert Dec 04 '18 at 19:14
  • 1
    You can use [`diff`](https://www.mathworks.com/help/matlab/ref/diff.html) (with *some tolerance* or you may end up asking [Why is 24.0000 not equal to 24.0000 in MATLAB?](https://stackoverflow.com/q/686439/)) – Sardar Usama Dec 04 '18 at 20:48

1 Answers1

2

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
Banghua Zhao
  • 1,518
  • 1
  • 14
  • 23
  • Good answer, but it would be better [with a tolerance](https://stackoverflow.com/questions/53615664/how-to-find-the-index-of-the-value-where-vector-elements-converge#comment94102732_53615664). – Cris Luengo Dec 05 '18 at 00:52
  • @CrisLuengo Thanks! You are right. Currently, the question didn't specify the tolerance so I assume the the converged value is equal to the last element. – Banghua Zhao Dec 05 '18 at 01:30