Approximate match between two matrices

조회 수: 12 (최근 30일)
Mohammed Rasul Iqbal
Mohammed Rasul Iqbal 2022년 2월 5일
댓글: Voss 2022년 2월 5일
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).

채택된 답변

Arif Hoq
Arif Hoq 2022년 2월 5일
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
Mohammed Rasul Iqbal
Mohammed Rasul Iqbal 2022년 2월 5일
Thank You Arif
Voss
Voss 2022년 2월 5일
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

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by