Finding Values nearest a certain number

4 views (last 30 days)
Brice Ortiz
Brice Ortiz on 19 Jun 2019
Edited: Shubham Gupta on 19 Jun 2019
I am trying to find the values in a set of data points nearest multiples of 1000 i.e 0, 1000, 2000, ..., 10000. I have an excel workbook with 4650 data points and lets say column one is load's (P) ranging from 0 to 10000 and column two are the deflections (d) caused by those loads. I need to find the values in column 1 closest to multiples of 1000 and then also find the corresponding values in column 2 that match the values from colunm 1.
So far I have,
X=[0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000];
y=min(abs(P-x))
This is returning the differences between the closest multiples and the loads. For example if the closest load in column 1 to 1000 is 1001.65 it is giving me the value 1.65 but I need it to give me the 1001.65 value. And then I would need help with another equation that would return the value in column 2 that corresponds to the load of 1001.65.

Answers (1)

Shubham Gupta
Shubham Gupta on 19 Jun 2019
Edited: Shubham Gupta on 19 Jun 2019
If I understand it clearly your P(and d) is a row/column vector of length 4650 and you need to find 11 points which are closest to X's 11 elements. If that is the case, I think you can try the code below to achieve the desired results. ( I am defining P as a row vector of 21 elements for this example )
X=[0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000];
P = [0,43,532,1046,1583,2043,2543,3095,3294,3794,3809,4130,4690,5021,5420,6043,6901,7800,8100,8900,10000];
d = rand(1,21);
%% Code that I tried
Abs_diff=abs(bsxfun(@minus,meshgrid(X,P),P'));
[mindiff,Corr_ind]=min(Abs_diff);
% Updated Load and deflection
P_updated = P(Corr_ind);
d_corresponding = d(Corr_ind);
I hope it helps !

Community Treasure Hunt

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

Start Hunting!