How to select the particular value and store ias a variable?

1 Ansicht (letzte 30 Tage)
Raja R
Raja R am 23 Mär. 2016
Beantwortet: Roger Stafford am 23 Mär. 2016
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.

Antworten (2)

dpb
dpb am 23 Mär. 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 Kommentare
Raja R
Raja R am 23 Mär. 2016
in my problem, c variable size is 5X3
1 45 0.12
2 50 0.35
3 75 0.65
4 60 0.85
5 95 0.95
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.85. In that case it goes to check that for y=0.65 the x value is 75 and for y=0.85 x is 60. The min value of x is 8 that is taken out and stored in new variable z. z=[4 60 0.85]
like so
dpb
dpb am 23 Mär. 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.

Melden Sie sich an, um zu kommentieren.


Roger Stafford
Roger Stafford am 23 Mär. 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

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by