Reverse 2D-lookup Table
Show older comments
I am having a lot of difficulty implementing this typ of controller in simulink.
To put it simply have a 2d-Lookup table a basically want to reverse engineer it. The 2d-lookup table takes in two input A and B and interpolates the corresponding output C in a large 79x101 matrix. I wish to define the output variable C for a fixed input variable A and find the corresponding variable B.
_____________
A--->| |
|2d-Looukup |--->C
B--->|___________|
____________
C--->| Reverse |
| lookup |--->B
A--->|___________|
What I have been attempting is to focus on first is the input A. So lets say for example that I have the following matrix.
0.4 0.5 0.6 0.7 (B)
____________________
17.9|67 89 95 108
(A) 18.0|74 92 110 123
18.1|80 97 115 127
18.2|84 106 119 135
(C)
I have an input of 18.046 for the input A. Now matlab will interpolate between 18.0
and 18.1 to give me the following output.
0.4 0.5 0.6 0.7 (B)
___________________
(A)8.046|76 94 112 125
(C)
So with the matrix output as shown below I define my desired output variable, e.g. C = 105. I then pass this variable C into a lookup table and matlab will interpolate the corresponding input variable B.
0.4 0.5 0.6 0.7
__________________
(C) 105--->|76 94 111 125| ---> 0.57 (B)
Could anyone help me in acheieving this problem as I have been stuck on it for quite some time now. Much appreciated
Owen
5 Comments
Image Analyst
on 6 Aug 2011
Is your function single-valued? Does each Z value have one unique (X,Y) location? For example, Z=rand(1000,1000) would have lots of locations (perhaps a million or more) that could be interpolated to have a value of 0.5.
Owen
on 7 Aug 2011
Pablo sanchez
on 25 Jul 2013
Dear, I had the same doubt, excellent, thank you very much,very gratefull..
marouan akhabbil
on 16 Mar 2022
Hello
i had the same issue i wanted to inverse a 2D lookup table, i tried the formula in the previous comments but it gives NAN values so i creted my own formula, it's so simple based on one interpolation :
here is my script
clc
nbrC1 = 3 ; %nombre d'elelements desiré dans le vecteur C1
A=[1 2 ];
B=[1 2 ];
C=[2 3 ; 3 4 ];
%objectif passer de : entrée sont A et B à entrées A et C
%calcul de la matrice B1 et des deux lignes A1 et C1
%A1=A inchangé
%etape 1 : les elements de la ligne C1
minC=min(min(C));
maxC=max(max(C));
precision=(maxC-minC)/(nbrC1-1) ;
C1=minC:precision:maxC ;
nA= size(A) ;
nA= nA(2) ;
nC1= size(C1) ;
nC1= nC1(2) ;
% etape 2 les valeurs de B1 ( matrice )
B1=zeros(nA,nC1);
for i= 1:nA
tmpC=C(i,:);
for j = 1:nC1
B1(i,:)=interp1( tmpC,B,C1, 'spline', 'extrap');
end
end
% the result
%B1
%C1
%A1 = A
%Marouan AKHABBIL
Michael Goebel
on 2 Sep 2022
This script works well, but not for me: A has a size of 8, B has a size of 32.
I set nbrC1 = 8
But it doesnt woork for me: C1 has weird values
How can i fix this?
Accepted Answer
More Answers (1)
Fangjun Jiang
on 8 Aug 2011
1 vote
To use non codegen supported functions, you need to declare eml.extrinsic('function_name'), but that is not what I am suggesting.
I recommend you creating a 2D lookup table data using the code above in MATLAB, making A and C as inputs, choosing 10 points for A and 9 points for C for example. You just need to do this once and then you can use a lookup table block in your Simulink model.
7 Comments
Guohong
on 6 Dec 2011
I've got the same issue however don't understand your words...What do you mean 10 points for A and 9 points for C? Sorry for the stupid question, new to simulink.
Fangjun Jiang
on 6 Dec 2011
The mention of 10 points or 9 points is just an example. Owen was trying to put the code in the answer into an Embedded MATLAB Function in a Simulink model. I was trying to say not to do that as the EMF is going to be executed at every time step. I expect this look-up table reversing only needs to be done once. Once you reverse the table and get the desired look-up table data, you can use the Simulink look-up table block to do the interpolation.
Guohong
on 7 Dec 2011
I understand when you said trying to avoid EMF, however didn't understand how to build the matrix with A, C as inputs. I presume if A is a 10 points array and C is a 9 one, we'll need B1 a 10X9 matrix, right?
Fangjun Jiang
on 7 Dec 2011
Yes, I usually try to make the number of rows and columns to be different so it's easier to identify column vs. row.
Guohong
on 8 Dec 2011
ok, but could you explain a little more in detail about how to make this B1 matrix? Thanks a lot.
Fangjun Jiang
on 8 Dec 2011
That is the code in the accepted answer. I've updated the answer to reflect a correction from the comments (due to the mix of row and column).
If that is not your use case, you may want to ask a separate question.
Guohong
on 8 Dec 2011
I think I got your idea. Just use lookup table dynamic to build another lookup table of Z and B, right?
Categories
Find more on Array and Matrix Mathematics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!