Approximate match between two matrices

i have two vectors C1 is calculated using code and A1 is input vector. C1(1x4) = [0.4311 0.123 0.011 0.441] , A1(5x4)=
X A B C
0.11 0.13 13.46 0.87
0.01 0.10 10.47 0.90
0.44 0.12 12.19 0.88
0.43 0.09 9.24 0.91
0.06 0.07 7.15 0.93
Need to find approximate match between individual C1 value with A1(:,1) and should give the coresponding output of A(:,2),A(:,3) & A(:,4).

 Risposta accettata

if there is no approximate value then it will return the minimum value of that column.
C1= [0.4311 0.123 0.011 0.441];
A1=[0.11,0.13,13.46,0.87;0.01,0.10,10.47,0.90;0.44,0.12,12.19,0.88;...
0.43,0.09,9.24,0.91;0.06,0.07,7.15,0.93 ];
A2=A1(:,1);
A3=A1(:,2);
A4=A1(:,3);
A5=A1(:,4);
[minValue1,nearestIndex] = min(abs(A2-C1(1,1)));
C1_first = A2(nearestIndex) % for the value C1=0.4311
C1_first = 0.4300
[minValue2,nearestIndex] = min(abs(A3-C1(1,2)));
C1_second = A3(nearestIndex) % for the value C1=0.123
C1_second = 0.1200
[minValue3,nearestIndex] = min(abs(A4-C1(1,3)));
C1_third = A4(nearestIndex) % for the value C1=0.011
C1_third = 7.1500
[minValue4,nearestIndex] = min(abs(A5-C1(1,4)));
C1_fourth = A5(nearestIndex) % for the value C1=0.441
C1_fourth = 0.8700

3 Commenti

Or
idx = interp1(A2,1:length(A2),C1(1,1),'nearest');
C1_first = A2(nearestIndex) % for the value C1=0.4311
Thank You Arif
As an alternative to the first method:
C1 = [0.4311 0.123 0.011 0.441];
A1 = [0.11,0.13,13.46,0.87; ...
0.01,0.10,10.47,0.90; ...
0.44,0.12,12.19,0.88;...
0.43,0.09,9.24,0.91; ...
0.06,0.07,7.15,0.93];
[min_diff,idx] = min(abs(A1-C1),[],1)
min_diff = 1×4
0.0011 0.0030 7.1390 0.4290
idx = 1×4
4 3 5 1
nearest_A1 = A1(sub2ind(size(A1),idx,1:size(A1,2)))
nearest_A1 = 1×4
0.4300 0.1200 7.1500 0.8700

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by