MATLAB: I need to compare a matrix with a static table

comparing

X= [11.9000 10.3000 9.5000
10.3000 8.7000 9.5000
9.5000 7.9000 9.5000
11.1000 9.5000 9.5000]
Y= [7.9 186.46
8.7 205.17
9.5 223.84
10.3 242.49
11.1 261.11
11.9 279.69]
Z=[7.9 196.39
8.7 216.11
9.5 235.79
10.3 255.45
11.1 275.07]
A=[9.5 247.74
10.3 268.4
11.1 289.03
11.9 309.62
12.7 330.19]
i need to comare the first column from X with the first column from Y
second column from X with the first column from Z
third column from X with the first column from A
and choose the corrsponding number from the second column from Y, Z and A correspondingly
RESULT =[279.69 255.45 247.74
242.49 216.11 247.74
223.84 196.39 247.74
261.11 235.79 247.74]

Best Answer

  • > [interp1(Y(:,1),Y(:,2),X(:,1)) interp1(Z(:,1),Z(:,2),X(:,2)) interp1(A(:,1),A(:,2),X(:,3))]
    ans =
    279.6900 255.4500 247.7400
    242.4900 216.1100 247.7400
    223.8400 196.3900 247.7400
    261.1100 235.7900 247.7400
    >>
    If you'll rearrange your naming scheme to use cell arrays or struct field names instead of multiple variables for A,Y,Z then you could write as a looping construct instead of duplicating same code with different variables.