Finding a noninteger value from an array
11 views (last 30 days)
I have an array which looks like:
x = linspace(0,1,101);
To find the index of the maximum point from an array, I can write:
[val,ind] = max(x);
However, I need the index of a particular point, let's say 0.70. How should i write the commands to find this?
My second question is that I have a specific value y = 0.703. I need to do my calculation using the array x, but I have to start from the value in x that is closest to y. Which means I am looking for the index of the point 0.70 in x, but i need to express it in terms of y in my code. How can I do that?
More Answers (2)
Matthew Murphy on 6 Jul 2016
Given the array x, you could also use the find command.
find(x == .7) --> ans = 71
For the second part, you could try a slightly different way with the same command.
Given some plus/minus window of .01, you could do this:
find(x < .703 + .01 & x > .703 - .01 ) --> ans = 71 72
Now, there are multiple values, so you could use a while loop with smaller and smaller margins until one value is found (with a catch to widen the margin if no values are found).
[I know this is an old post, but I still wanted to follow up with an approach that worked for me.]