Find adjacent values of a vector corresponding to another vector

Hi, I am trying to find the adjacent points of a 1-D vector corresponding to the elements of another column vector. It is very hard to explain. I will explain it from an example.
A=[ 11; 22; 32; 44; 51;]; B=[11.4 32.4 36.6]; So I need a matrix with the adjacent points of each element of the vector B of vector A as in, Ans1=[11 22; 32 44; 32 44;] also the corresponding locations as in, ans2[1 2; 3 4; 3 4;] Appreciate any help.

 Risposta accettata

idx=discretize(B,A);
Ans2=[idx(:),idx(:)+1];
Ans1=A(Ans2);

7 Commenti

Your answer worked for my example. But it doesn't work for my data set. Let me get you my data set.
A=[1
1
0.71100003
0.62000000
0.53899997
0.46700001
0.40200001
0.34500000
0.29499999
0.25299999
0.21699999
0.18600000
0.16000000
0.13699999
0.11700000
0.099799998
0.085000001
0.072499998
0.061799999
0.052600000
0.044700000
0.037999999
0.032299999
0.027400000
0.023200000
0.019700000
0.016700000
0.014100000
0.011900000
0.0099799996
0.0083999997
0.0070600002
0.0059400001
0.0049999999
0.0042099999
0.0035500000
0.0029899999
0.0025200001
0.0021200001
0.0018000000
0.0015200000
0.0012900000
0.0011000000
0.00094200001
0.00080699997
0.00069299998
0.00059700001
0.00051400001
0.00044300000
0.00038200000
0.00033000001
0.00028599999
0.00024800000
0.00021499999
0.00018800001
0.00016400000
0.00014300000
0.00012600000
0.00011000000
9.6999996e-05];
B=[0.986923267000000
0.672382994000000
0.458089201000000
0.312092540000000
0.212626172000000
0.144860520000000
0.0986923270000000
0.0672382990000000
0.0458089200000000
0.0312092540000000
0.0212626170000000
0.0144860520000000
0.00986923300000000
0.00672383000000000
0.00458089200000000
0.00312092500000000
0.00212626200000000
0.00144860520000000
0.000986923000000000
0.000672383000000000
0.000458089000000000
0.000312093000000000
0.000212626000000000
0.000144860000000000
9.86923000000000e-05];
Thank you in advance.
Matt J
Matt J il 22 Dic 2017
Modificato: Matt J il 22 Dic 2017
This A data is unsorted. Your problem is ill-defined for unsorted A. What would "adjacency" mean in that case?
Yes. Because these are pressure levels in the atmosphere. Can I do it without sorting it? Adjacent means the values nearest or closet. If you find a value in between two values I call those two values adjacent.
You should modify your posted question with a similarly small example with unsorted A. How can a value of B fall "in between two values" of A if A is not sorted?
I am sorry. I meant by not sorted that there can be NaN values and repeated values in the vector. But initially I don't have to worry about it. But sure, I can flip the matrix and make it monotonically increasing. But vector A is monotonically decreasing.
Once you flipped the matrices it works. Thanks. :)

Accedi per commentare.

Più risposte (1)

Here is a solution that finds the element in A that is just before or just after each element in B.
A=[ 11; 22; 32; 44; 51;]; B=[11.4 32.4 36.6]
clear ans2
ans2(:,1) = nearestpoint(B, A, 'before') ;
ans2(:,2) = nearestpoint(B, A, 'after') ;
ans1 = A(ans2) % will fail when elements of B do not fall between elements of A. Check for that!
My NEARESTPOINT utility can be downloaded from the File Exchange:

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by