Hello, I have an array with 20 values of steps per minute. I already know that the perfect outcome of one of these values is 33spm. But unfortunately 33spm is not in the array 34.8 is which is the closest to 33. What is the code to find the value closest to 33?
The ideal answer would be:
ClosestValue = 34.8
Could someone help me please?

 채택된 답변

Birdman
Birdman 2018년 1월 5일
편집: Birdman 2018년 1월 5일

23 개 추천

Try the following approach:
a=[34.8 31 29 26.7 39.5];%dummy data
n=33;
[val,idx]=min(abs(a-n));
minVal=a(idx)
Edit after Jan's warning(multiple values)
a=[34.8 31.2 29 26.7 39.5];%dummy data
n=33;
[~,~,idx]=unique(round(abs(a-n)),'stable');
minVal=a(idx==1)

댓글 수: 17

Jens Keijser
Jens Keijser 2018년 1월 5일
Thank you!
Jan
Jan 2018년 1월 5일
@Jens: What should happen, if multiple values have the same distance from the searched number? Birdman's code replies the first occurrence, which might be sufficient.
@Birdman: Alternatively without rounding:
dist = abs(a - n);
minDist = min(dist);
idx = find(dist == minDist);
Now minDist is a scalar, while idx contains all indices belonging to this value.
Rob
Rob 2020년 4월 9일
@Jan: Your code without rounding has the problem of hidden Round-off Errors. Thus, the result for this example is only the index 1 and not 1 and 2.
Ganesh Kini
Ganesh Kini 2020년 4월 16일
Hi,
I have a similar scenario
Does this work if i have an array whose matrix of 7 dimension ?
For example - peiod_arr(2,1,10,10,15,11,8)
Please let me know
Yes, no matter how many dimensions the array has, you can use the strategy
[min_dift, idx] = min(abs(TheArray(:) - TargetValue));
closest_value = TheArray(idx);
You might also want to get the indices:
[indices{1:ndims(TheArray)}] = ind2sub(size(TheArray), idx);
With an array that large, the possibility tends to grow that you might have multiple locations that are all the same distance. You should then consider:
dist = abs(TheArray - TargetValue);
min_dist = min(dist(:));
idx = find(dist == min_dist);
[indices{1:ndims(TheArray)}] = ind2sub(size(TheArray), idx);
Ganesh Kini
Ganesh Kini 2020년 4월 17일
Hi Walter,
I tried the code, and its not working as expected
I am not getting the value present in the array, I am getting the target value itself as the output.
COuld you please suggest some alternate solution?
Walter Roberson
Walter Roberson 2020년 4월 18일
peiod_arr(idx)
Ganesh Kini
Ganesh Kini 2020년 4월 18일
편집: Ganesh Kini 2020년 4월 18일
Hi Walter,
The thing is that I am actually dealing with decimal numbers and i want it to work for 0.001 precision.
For example if i have a target value of 26.145 and my period_arr has a value of 26.147 it should able to retrieve the value.
But it is not working as expected .
Please suggest
Walter Roberson
Walter Roberson 2020년 4월 18일
please post your current code. The code that I guessed that you had worked properly for me.
Ganesh Kini
Ganesh Kini 2020년 4월 28일
편집: Ganesh Kini 2020년 4월 28일
abc = period_fun(2,1,2,5,5,13,8);
%finding the nearest possible value
dist = abs(period_fun - abc);
[min_dist, idx] = min(dist(:));
nearestvalue = period_fun(idx);
actualidx= find(period_fun ==nearestvalue,1);
[p1,p2,p3,p4,p5,p6,p7] = ind2sub(size(period_fun), actualidx);
v1 = nw_vec(p5);
First Case
v1 is displaying as follows
0.80000 0.65000 0.75000
all these are in the nw_vec array but 0.8 is expected answer. I should get only 0.8 as the answer
Second case
v1 is displaying as follows
0.40000 0.55000
0.2 is expected but its not giving it as output. I should get only 0.2 as the answer
nw_vec is .vec file
the file is as follows
0.2, 0.36, 0.38, 0.40, 0.47, 0.5, 0.55, 0.65, 0.75, 0.8
I want only the p5 value for v1 = nw_vec(p5)
kindly help me
Walter Roberson
Walter Roberson 2020년 4월 28일
For first case, where you are getting three values, then what is size(actualidx) ? With the code you used, with find() with 1 as the second input, you should only get back one output, so ind2sub() should only be returning scalars. What is size(p5) ? What is class(nw_vec) ?
Hi! I am checking @Birdman's second answer, and in case I introduce in the series the value I am looking for explicitly, something does not work well.
a=[34.8 31.2 33 29 26.7 39.5];%dummy data
n=33;
[~,~,idx]=unique(round(abs(a-n)),'stable');
minVal=a(idx==1)
minVal =
34.8000 31.2000
Someone knows what happens?
Regards!
@Jon Martínez Rico: The 2nd code in this answer does not work. Use the simpler version:
dist = abs(a - n);
minDist = min(dist);
minIdx = (dist == minDist);
minVal = a(minIdx)
Yeah, I did it @Jan. It was just to check if I was missing something.
Thank you for your quick answer :)
Umesh Gautam
Umesh Gautam 2023년 8월 29일
편집: Umesh Gautam 2023년 8월 29일
Thanks @Birdman, code is working great...even one can find the array of values.
Andres Felipe
Andres Felipe 2023년 12월 1일
Thankss.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2018년 1월 5일

3 개 추천

Use interp1 with 'nearest'
Or since the vector is small abs() the difference between the probe and the fixed values and min() that and take the second output of min() and use that to index the fixed values. This is not as convenient as interp1 but should be faster
Atique Barudgar
Atique Barudgar 2019년 11월 8일

0 개 추천

I am not clear how Birdman SIr's answer came
when idx =1
then how a(idx)=34.8
I didnt got how and what is idx

댓글 수: 1

Nicolas Hofer
Nicolas Hofer 2021년 10월 8일
idx stand for index. search for indexing in matlab for further explanation

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2018년 1월 5일

댓글:

2023년 12월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by