MATLAB Answers

2D array interpolation

5 views (last 30 days)
Carl St-Laurent
Carl St-Laurent on 1 Jul 2020
Commented: Walter Roberson on 1 Jul 2020
Hi everyone,
I want to interpolate this array to generate values for new values in X for example 120. I'm a bit lost, what would be the best way to do that ?
Thanks for your help !
0 0,2 0,8 1 1,3 1,5 2 2,5 3 4 6 10 15 20 30 40 50 60 70 80 90 100 110 120
1000 0 150 207 306 351 384 437 482 537 634 746 990 1376 1782 2860 4120 5600 6660 7000 7000 7000 7000 7000
2000 0 134 170 238 273 300 341 382 410 457 530 690 1001 1348 1997 2656 3571 4653 5233 5376 5520 5633 5633
2500 0 125 153 203 235 260 292 331 347 381 462 600 834 1080 1600 2128 2800 3571 4008 4358 4665 4767 4767
3000 0 122 145 180 201 226 264 288 308 335 411 537 713 914 1342 1793 2325 2980 3384 3795 4140 4280 4280
4000 0 120 137 154 177 191 226 251 272 286 343 443 570 710 1013 1346 1700 2050 2400 2750 3065 3167 3227
5000 0 119 132 148 164 177 200 219 240 263 303 380 493 594 830 1086 1364 1647 1925 2209 2478 2600 2660
6000 0 117 127 140 157 168 184 200 216 240 282 336 428 522 715 915 1140 1380 1628 1850 2065 2167 2227
7000 0 116 126 138 152 163 176 191 202 219 262 313 392 462 625 802 975 1171 1418 1596 1751 1872 1932
8000 0 115 123 136 148 160 170 183 192 205 245 297 360 422 566 725 870 1035 1266 1417 1549 1623 1683
9000 0 115 123 134 146 158 167 178 186 197 228 280 333 392 521 660 796 943 1153 1287 1393 1440 1510
10000 0 115 123 134 144 156 165 176 182 194 218 267 316 372 490 619 739 870 1046 1172 1274 1334 1414
11000 0 114 123 134 144 154 163 174 181 191 211 255 305 356 462 578 690 807 951 1070 1168 1246 1372
12000 0 112 124 134 144 152 162 173 180 190 206 245 295 344 439 546 652 756 879 988 1079 1160 1361
14000 0 111 126 136 144 152 162 171 178 187 199 228 278 322 405 490 592 679 784 875 964 1037 1210
16000 0 110 131 138 144 152 161 170 177 185 196 215 262 304 380 450 538 622 706 796 894 955 1057
18000 0 110 136 144 149 154 162 172 178 186 195 205 247 291 359 421 497 577 653 730 830 874 937
20000 0 110 146 156 161 166 170 177 182 189 194 200 238 280 343 400 467 541 615 690 760 802 862

  5 Comments

Show 2 older comments
Walter Roberson
Walter Roberson on 1 Jul 2020
Ah, I did not scroll far enough. I will look again.
Walter Roberson
Walter Roberson on 1 Jul 2020
So for 120 you want to find the collection of locations
1000 0.16
2000 0.179
2500 0.192
3000 0.197
4000 0.2
5000 0.246
6000 0.38
7000 0.44
8000 0.575
9000 0.575
10000 0.575
11000 0.6
12000 0.6
14000 0.56
16000 0.486
18000 0.431
20000 0.367
As those are all the locations that can lead to 120 by linear interpolation along the rows?
Or should diagonal influences also be included? The
120 137
119 132
block implies that there is a line or curve of 120 formed by mixes of those values
Carl St-Laurent
Carl St-Laurent on 1 Jul 2020
Just along the rows !

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 1 Jul 2020
Assuming that array is stored in data
vals_to_find = [120 140];
nvals = length(vals_to_find);
D = data(2:end,2:end);
X = data(2:end,1);
Y = data(1,2:end);
D = [D,D(:,end)]; %deliberately duplicate last column makes some tests easier.
nr = size(D,1);
Yvals = zeros(nr,nvals);
for row = 1 : nr
thisrow = D(row,:);
pos = find(diff(thisrow)==0,1);
Yvals(row, :) = interp1(thisrow(1:pos), Y(1:pos), vals_to_find);
end

  2 Comments

Carl St-Laurent
Carl St-Laurent on 1 Jul 2020
Thanks Walter, and what would be the proper way to import data including both axises in a single array ?
Walter Roberson
Walter Roberson on 1 Jul 2020
data = load('data.txt'); %or whatever the name of your file is
Or perhaps the answer you are looking for is
Both = [X, Yvals];
This would give the X (such as 4000) in the first column, and the interpolated Y for each vals_to_find in subsequent columns.

Sign in to comment.


Translated by