How to select the particular value and store ias a variable?
1 view (last 30 days)
Show older comments
I have x values as
[2 4 6 10 8]
and coressponding y values as
y = [0.25 0.45 0.55 0.65 0.75]
Now,i allow to generate random number. Suppose i get a number as 0.675. Then this 0.675 is going to be searched in y variable. but there is two value closer to that, that is 0.65 and 0.75. In that case it goes to check that for y=0.65 the x value is 10 and for y=0.75 x is 8. The min value of x is 8 that is taken out and stored in new variable z.
0 Comments
Answers (2)
dpb
on 23 Mar 2016
abs(0.65-0.675) = 0.025
abs(0.75-0.675) = 0.075
so it's not clear how you want 8 as the result instead of 10 as it is the nearest, clearly.
Perhaps you mean the first location for which
>> x(find(y>0.675,1,'first'))
ans =
8
>>
maybe??
2 Comments
dpb
on 23 Mar 2016
That's simply repeating the same description which doesn't make sense...in what manner, specifically, is "two value closer to that, that is 0.65 and 0.85" to be interpreted so that is, indeed so?
The lookup shown earlier works as well if you simply search over the appropriate column of c and select the appropriate indices for whichever values you wish in the end result. The problem is understanding how you actually determine which is the proper row to choose as clearly it is not the closest in absolute difference.
Roger Stafford
on 23 Mar 2016
r = rand;
t1 = (y>=r);
t2 = (y<=r);
if any(t1)
[~,i1] = min(y(t1));
if any(t2)
[~,i2] = max(y(t2));
z = min(x(i1),x(i2));
else
z = x(i1);
end
else % If t1 was empty, then t2 will not be
[~,i2] = max(y(t2));
z = x(i2);
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!