how to pick the closest value to a point using ismember or anything else?

4 次查看(过去 30 天)
Hi all,
I have the following code, my problem is that multiple values that result in the output (CurRatio) fall within my specific range but I want to pick/save only one value for each range (RatioEins,zwei,...). Preferably, that pick would be the closest to the middle of the range (I want something instead of ismember that can pick the best values that its output is closest to the middle of the ranges). For instance, for RatioEins, my pick of data would be best to be the one that its output is closes to 0.25. If not possible, then the first value within the range will work too. Could you please teach me to get that?
'(Parts are removed due to the data sensivity)'
RatioEins = 0.24:0.0001:0.26
RatioZwei = 0.49:0.0001:0.51
RatioDrei = 0.74:0.0001:0.76
AllRatios = ([RatioEins,RatioZwei,RatioDrei])
for i = 1:100
'bunch of codes here output is CurRatio'
CurRatio = round(WinShapeArea./MainShapeArea,2)
if ismember(CurtRatio,AllRatios)
MalEin(1,i)= ([NewX])
MalZwei(2,i) = ([NewY])
else
MalFragen(1,i)= [NewX]
MalFragen(2,i) =[NewY]
end
end
  2 个评论
Wolfgang McCormack
Wolfgang McCormack 2021-3-13
I think I need to create a new for loop for checking that from outputs and recalculating but I truly have no ideas! :D
Wolfgang McCormack
Wolfgang McCormack 2021-3-13
there is something else that I can add, since the decomals are only 2, there are multiple values that results in 0.25. So, really doesn't matter which one we pick although still the middle one would be the best.
So if I have an output array that it's heading shows its yielding range, then how I can copies of that range column wise, something like this:
3 5 5 5 5 6 6 6
V V V V V V V V
imaging Vs are the values that resulted in the above row of numbers. How can I keep only the first column that its heading or top value is 5 and remove the rest of columns that their headings are 5. Thanks!

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-3-13
The general strategy is often to take a coordinate and subtract off the ideal value, take the absolute value, and min(), taking the second (index) element. Use the index on the original values to retrieve the one that was closest.
  3 个评论
Walter Roberson
Walter Roberson 2021-3-13
[~, IA] = unique(headingvalues, 'table'); %find first occurance of each unique value
headingvalues = headingvalues(IA);
data = data(:,IA); %filter down

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by