# Reverse 2D-lookup table: issues iterating through loops

20 views (last 30 days)
Johanan Agarwal on 3 Jul 2020
Commented: Johanan Agarwal on 9 Jul 2020
Hi,
I am trying to implement a reverse 2d-lookup table as described in this thread:
The accepted answer provides this code for finding an input B1 at a specified A1 and C1 input:
A=[17.9 18.0 18.1 18.2]';
B=[0.4 0.5 0.6 0.7];
C=[67 89 95 108
74 92 110 123
80 97 115 127
84 106 119 135];
A1=18.046;
%Z=interp2(A,B,C,A1,B);
Z=interp2(A,B,C',A1,B);
C1=105;
B1=interp1(Z,B,C1);
I am trying to do a similar thing with inputs p,d, trying to get output s which I have stored in the variable so
[P,S] = meshgrid(54020:100:101320,0:10:3000);
d = -100.*(((((1373.393./4.002602).*8.3144598).*300)./((P+S).*0.4569)).*((log(P./(P+S)))-(P./(P+S))+1));
so = zeros(301, 474);
p = 54020:100:101320;
p = p';
s = 0:10:3000;
for i = 1: size(p)
%Z=interp2(A,B,C,A1,B);
Z=interp2(p,s,d,p(i),s);
for j = 1:size(s) -1
so(i,j)=interp1(Z,s,d(i,j));
end
end
I initialized the so variable as a matrix of zeros in the start to avoid a warning saying the size of so will be increasing throughout the iteration of the loops.
However, when running this code, so remains unpopulated and remains as a matrix of zeros
I am not sure why this is the case. Any help is much appreciated.

Jyotsna Talluri on 7 Jul 2020
In your code provided, size of array s is [1 301].
for j = 1:size(s)-1 % first element of the size array is considered
In above line j is empty row vector due to which loop excecution does not take place and hence the array 'so' remains as it is initialized.
Try replacing your code with below lines.
for i = 1:length(p)
%Z=interp2(A,B,C,A1,B);
Z=interp2(p,s,d,p(i),s);
for j = 1:length(s)
so(i,j)=interp1(Z,s,d(j,i));
end
end
##### 1 CommentShowHide None
Johanan Agarwal on 9 Jul 2020
Thank you so much, it works.