Table lookup based on 3 variables
Afficher commentaires plus anciens
Hello,
I am trying to take a large set of data and first create a table based on that data then perform a lookup to find my desried values based on 3 variables of the table.
The data begins as:
A: [10 89 23 4 700 8]
B: [1 234 34 6 89 77]
C: [-12 23 9 -400 62]
D:Z: Variables that will need to be looked up (All variables have the same number of columns as A, B & C)
Then I space out the vectors A, B & C and grid them:
A_min = min(A); A_max = max(A); gridsize = 250;
a = linspace(A_min, A_max, gridsize)
b = linspace(B_min, B_max, gridsize)
c = linspace(C_min, C_max, gridsize)
[A_grid, B_grid, C_grid] = meshgrid(a, b, c)
Then I try to grid the rest of the data to form a table of all the values based on the 3 gridded variables
output_D = griddata(A', B', C', D', A_grid, B_grid, C_grid, 'linear')
Which results in a output that is all NaN
I have been successful in doing this based off 2 variables
A_min = min(A); A_max = max(A); gridsize = 250;
a = linspace(A_min, A_max, gridsize)
b = linspace(B_min, B_max, gridsize)
[A_grid, B_grid] = meshgrid(a, b)
output_D = griddata(A', B', D', A_grid, B_grid, 'linear')
3 commentaires
If i've understood correctly I've solved a similar problem doing this:
Where you create a second table (nt) where the rows in nt are only those in t that satisfy the criteria for columns A,B,C you could expand this further if needed also.
You can also use < or > as necessary in place of ==.
I'm not sure if there is a faster or better way, but this has worked well for me. I usually use this for filtering a table to a specific range of data. If there is more than one result it would show all the possible combinations in table nt.
%Create random 5x5 array
x = magic (5);
%Conver array to table
t = array2table(x);
% Give table variable names
t.Properties.VariableNames = {'A','B','C','D','E'};
% Create new table based on criteria
nt = t(t.A == 17 & t.B == 24 & t.C == 1,:);
% Results
nt.D
nt.E
"Which results in a output that is all NaN"
How did you check this? When I run your code (after a few minor bug fixes, e.g.C size, missing D, etc.), about sixteen percent of the values are not NaN:
>> numel(output_D)
ans =
15625000
>> nnz(~isnan(output_D))
ans =
2483115
Most of your grid values are outside the convex hull of the input data, which according to the griddata documentation should therefore be returned as NaN. As far as I can tell, everything is working exactly as expected and documented.
Note that you should probably use ndgrid rather that meshgrid.
Note that scatteredInterpolant is required if you want to perform extrapolation.
John
le 27 Juin 2019
Réponses (0)
Catégories
En savoir plus sur Interpolation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!