How to find two values in a 3D table?

1 view (last 30 days)
WPG
WPG on 12 Dec 2022
Edited: WPG on 13 Dec 2022
Hello ML experts,
I have the two tables below and I need to find the X and Y values from the first table in the second table to return the Z value. For example, for the variable a, searching for Y=31 and X=3.7 in the second table should return the Z value as a result of an interpolation of X between 3 and 4 and Y between 30 and 40. Please any idea how to get it?
First table Second table
Y X 1 2 3 4 5 6
a 31 3.7 10 16 17 17 17 19 17
b 22 3.4 20 23 22 22 22 24 24
c 55 2.7 30 31 29 28 27 29 31
d 23 4.5 40 38 36 35 34 34 35
e 11 2.5 50 48 47 46 44 44 42
60 59 58 57 56 54 48
Thanks!

Accepted Answer

David Hill
David Hill on 12 Dec 2022
B=[ 16 17 17 17 19 17
23 22 22 22 24 24
31 29 28 27 29 31
38 36 35 34 34 35
48 47 46 44 44 42
59 58 57 56 54 48];
A=[31.0000 3.7000
22.0000 3.4000
55.0000 2.7000
23.0000 4.5000
11.0000 2.5000];
[x,y]=meshgrid(1:6,10:10:60);
for k=1:size(A,1)
z(k)=interp2(x,y,B,A(k,2),A(k,1),'linear');
end
z
z = 1×5
28.0000 23.1200 51.8000 24.5000 17.5000
  1 Comment
WPG
WPG on 12 Dec 2022
David, pls, what are the values 10:10:60? I took some new tables with new values and size... Thanks.

Sign in to comment.

More Answers (2)

Bora Eryilmaz
Bora Eryilmaz on 12 Dec 2022
Edited: Bora Eryilmaz on 12 Dec 2022
N-dimensional interpolation should probably work. For more details, see interpn.
X = [1 2 3 4 5 6]';
Y = [10 20 30 40 50 60]';
V = [16 17 17 17 19 17; ...
23 22 22 22 24 24; ...
31 29 28 27 29 31; ...
38 36 35 34 34 35; ...
48 47 46 44 44 42; ...
59 58 57 56 54 48];
x = 3.5;
y = 31;
interpn(X,Y,V,x,y)
ans = 31.4000

WPG
WPG on 13 Dec 2022
I added new tables and numbers, but I getting these errors from ML....
clear all
close all
clc
% Main Table
B=[0 0 -0.5 -1 -1.440000057 -1.784596801 -3 -5 -7.105848789 -8.260867119 -9.185858727 -9.449726105 -9.40335083 -12.37765598 -15.51675701 -16.7570076 -13.966465
1 1.159999967 0.579999983 0 0 -0.054761443 -1.132842064 -1.919999957 -3.829999924 -5.754557133 -6.664733887 -6.920663834 -6.784352779 -9.147246361 -11.88738346 -13.4312067 -11.2360363
1.234512448 1.431022644 1.899999976 2 2.331813335 2.400000095 2.5 2.5 2.5 1.467604995 1.902814865 0.855291605 1.02640295 -0.914024711 -2.865585566 -3.10555625 -0.364678293
2.217190742 2.238109112 3.349999905 3.599999905 4.75 4.849999905 5.050000191 4.474590302 4.607336521 4.740082264 4.872828484 6.129863262 8.508735657 6.507114887 4.602336407 2.752496481 4.535929203
3.392516613 3.483454227 5.14729023 6.199999809 7.099999905 7.349999905 7.358082771 7.984946251 7.37113905 7.36021471 7.927046776 9.201024055 11.42355633 14.32373142 12.34190559 8.634778976 9.443357468
4.887710094 5.001031399 6.93799305 7.682701588 10.95540333 12.23428249 12.26078033 12.28381634 11.23390579 11.29238987 11.01430321 12.27508259 14.34490681 17.14698792 20.24068069 14.84707737 14.69777203
6.554571629 6.762286663 8.957401276 9.79179287 14.05780029 15.90397263 16.52565002 16.52229309 15.40787888 15.61107922 15.91137886 16.08626556 17.26742744 19.97024345 22.8639183 21.07211494 19.94839859
8.376939774 8.677400589 11.10476112 11.78483963 17.16792488 19.70735168 20.99146461 20.74003601 20.18478012 19.75080109 19.94654083 21.21051025 22.0176754 23.37398338 25.48715591 27.30198669 25.18423271
10.35123062 10.73229408 13.42176342 13.77788639 20.238657 24.65418816 25.25714684 25.51137543 24.70775604 24.0808506 24.14662552 25.06968117 26.42211342 28.01449203 28.73020172 33.5279007 30.42006683
12.46657276 12.93490505 15.96307564 15.77100563 23.71339607 29.31688118 29.84448242 30.73209763 29.28995132 29.77126694 28.75317192 29.48318672 30.72710228 31.82641983 33.13222122 35.92963409 35.65589905
15.87590981 16.50312614 19.99935341 18.79130173 30.8146286 35.73960876 39.53637695 38.49571609 38.25179291 37.43046188 37.54821396 38.37471008 37.47976685 38.05853271 37.5624733 40.10954666 43.39770126
19.53913879 20.33345032 24.24980354 21.81160927 36.94875336 42.27612686 47.64330292 46.93922806 46.72492981 47.21810532 45.76124954 46.29708099 46.88663483 44.53410721 43.14448547 43.43746185 51.0931282
23.44340134 24.3995266 28.69225502 24.82585335 39.20000076 48.37068558 55.91779709 58.27306747 55.69072342 56.28728867 56.22953415 54.92830276 54.74085617 52.4981308 48.92195129 47.76531982 52.70474625
31.93018532 33.20989609 38.18613434 30.83511543 44.25 55.30439758 64.93103027 66.36563873 64.40306091 64.85644531 75.62831116 72.90232086 71.87582397 70.97090149 64.953125 56.41384125 60.20088959
41.24858475 42.84879684 48.43244553 36.84437561 58.20000076 65.67229462 73.12511444 74.45821381 73.11539459 73.42560577 80.79582977 78.93067169 78.22834015 77.60918427 73.49175262 67.649086 67.87563324
51.33988953 53.25222015 59.37031174 62.29718018 78.01433563 81.40711212 84.92370605 82.55078125 81.82772827 81.99476624 85.96334839 84.95903015 84.58084869 84.24745941 82.03038025 78.88433075 79.00631714
91.99211884 91.99211884 91.99211884 91.99211884 91.99211884 91.99211884 91.99211884 91.99211884 91.99211884 91.99211884 91.99211884 91.99211884 91.99211884 91.99211884 91.99211884 91.99211884 91.99211884];
% Result table
A=[0 0
0 0
0 0
6.671875 18.03922081
33.03125 25.49020004
50.8125 20.39216042
57.45312119 9.41176796
58.39062119 4.70588398
57.45312119 3.529412985
57.14062119 2.745099068
57.03125 8.235297203
56.51562119 5.882355213
52 0
32.14062119 0
6.953125 0
3.046875 16.07843971
27.64061928 21.17647934
43.98437881 14.50981045
50.95312119 14.11765003
55.65625 12.15686989
57.3125 0.392156988
57.79687881 13.72550011
59.5625 10.58823967
60.1875 6.666668892
60.64062119 10.98040009
64.46875 15.29411983
70.65625 17.25490952
73.28125 14.90196991
75 13.72550011
75.89061737 10.58823967
76.25 12.15686989
76.625 10.19608021
76.03125 4.70588398
75.9375 13.33333969
77.53125 12.94118023
79.34375 15.29411983
80.59375 12.15686989
80.67188263 6.274511814
79.42188263 3.137255907
77.07811737 7.843140125
76.875 12.54901981
77.3125 10.19608021
66.53125 0
63.70312119 12.15686989
68.85938263 21.96079063
72.92188263 12.54901981
75.17188263 16.47059059
77.53125 14.90196991
78.1875 1.960785031
74.14061737 8.235297203
73.03125 0
67.67188263 16.8627491
70.85938263 7.843140125
71.85938263 15.29411983
71.34375 1.568627954
69.85938263 6.274511814
70.29688263 12.54901981
66.23438263 0
67.71875 21.56863976
71.29688263 15.29411983
74.20311737 17.25490952
76.25 8.627453804
75.5625 9.803924561
75.71875 12.15686989
77.03125 12.15686989
77.04688263 12.94118023
79.39061737 16.47059059
78.82811737 8.627453804
77.04688263 2.35294199
74.54688263 7.450983047
72.375 4.313726902
68.23438263 0
56.76562119 0
49.6875 0
48.125 23.52942085
62.45312119 25.88236046
69.8125 18.43137932
73.09375 12.54901981
74.48438263 13.72550011
77.42188263 21.17647934
82.98438263 22.35294914
87.32811737 26.27452087
92.17188263 16.47059059
94.14061737 18.43137932
95.48438263 14.11765003
94.42188263 10.19608021
93.26561737 9.019611359
92.20311737 10.58823967
91.625 12.15686989
91.40625 10.58823967
90.40625 12.54901981
90.85938263 16.47059059
92.75 16.07843971
92.09375 3.529412985
91.09375 18.43137932
93.23438263 18.43137932
93.96875 12.15686989
93.45311737 12.94118023
94.79688263 22.35294914
96.85938263 12.54901981
95.85938263 8.627453804
94.42188263 12.15686989
94.20311737 8.235297203
93.82811737 13.72550011
94.0625 13.33333969
93.85938263 10.98040009
93.92188263 14.11765003
94.14061737 14.11765003
94.14061737 10.98040009
91.85938263 7.843140125
89.40625 9.41176796
88.67188263 12.15686989
88.75 13.72550011
88.4375 13.33333969
88.09375 12.15686989
87.84375 9.41176796
87.4375 9.41176796
87.04688263 13.33333969
88.1875 19.21569061
90.46875 18.03922081
90.48438263 16.47059059
90.26561737 13.72550011
90.82811737 13.72550011
91.125 12.54901981
90.48438263 5.490198135
89.09375 10.58823967
88.23438263 9.41176796
86.21875 4.313726902
84.5 6.666668892
84.01561737 12.15686989
83.26561737 7.843140125
80.625 0
77.53125 9.803924561
79.25 9.019611359
78.84375 2.745099068
77.10938263 11.76471043
79.34375 10.58823967
75.3125 0
76.85938263 29.41176987
85.125 19.60784912
88.125 14.50981045
89.51561737 10.19608021
80.67188263 0
75.90625 12.54901981
79.67188263 20.39216042
83.8125 19.21569061
82.8125 5.882355213
83.875 16.8627491
85.01561737 7.450983047
84.5625 13.72550011
83.78125 7.450983047
81.21875 4.70588398
80.35938263 12.94118023
83.25 14.90196991
86.23438263 20.39216042
89.01561737 20.78432083
91 17.25490952
92.9375 20.00000954
95.71875 17.25490952
93.28125 8.235297203
90.26561737 3.921570063
86.57811737 2.745099068
81.95311737 0
73.59375 0
51.51562119 0
31.48438072 0
11.375 0
0 0
0 0];
[x,y]=meshgrid(1:17,338:338:100);
for k=1:size(A,1)
z(k)=interp2(x,y,B,A(k,2),A(k,1),'linear');
end
Folowing the errors
Error using griddedInterpolant
Grid arrays must have NDGRID structure.
Error in interp2>makegriddedinterp (line 228)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 136)
F = makegriddedinterp(X, Y, V, method,extrap);
Error in Matlab2020_D2D_torque_output_compare (line 201)
z(k)=interp2(x,y,B,A(k,2),A(k,1),'linear');

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!