Filter löschen
Filter löschen

Accurately obtaining the value of a variable at the requested points

59 Ansichten (letzte 30 Tage)
Richard Wood
Richard Wood am 31 Jul. 2024 um 2:27
Beantwortet: Divyam am 6 Aug. 2024 um 9:19
Dear all
I am attaching a data set, where the first and second columns represent spatial coordinates (x,y) in two-dimensional space, while the third column shows the value of a given magnitude at the aforementioned set of (x,y) points. I was wondering which could be the best option in a scenario inside a for loop, where the (x,y) variables change at each step, to obtain an accurate value for the aforementioned variable defined in the third column of the attached file, even if for that combination of (x,y) coordinates the value of the variable is not present a priori. Would be something like
griddata(file(:,1),file(:,2),file(:,3),x,y,"cubic");
a quick and accurate approach?
  1 Kommentar
Ayush Modi
Ayush Modi am 31 Jul. 2024 um 2:46
Bearbeitet: Ayush Modi am 31 Jul. 2024 um 3:07
Hi Richard,
Your data is gridded. It is better to go with griddedInterpolant method.
Refer the following MathWorks documentation for more information on griddedInterpolant function:
To learn more about different interpolation methods in griddedInterpolant function, refer the following section:
Note - You can optimize your code, by using vectorization to make a single call to the function instead of making multiple calls from inside a for loop.
Refer to the following MathWorks documentation to know more about vectorization:

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Divyam
Divyam am 6 Aug. 2024 um 9:19
Interpolation can be used in this scenario to figure out the unknown values. To run either 'griddata' or 'griddedInterpolant' there must be enough data entries for each grid dimension which is not the case in the data you provided. In scenarios like these, you can either use the 'reshape' function to reshape your data or use 'scatteredInterpolant' for scattered interpolation.
%% Preprocessing the data to get unique values for x and y
T = readtable('file.txt','Delimiter',' ');
arr = table2array(T);
[r,c] = size(arr);
% This loop can be edited to get unique values for (x,y) pair
for i=1:200
x(i) = arr(2*i+200*i-1,1);
y(i) = arr(2*i+200*i-1,2);
mag(i) = arr(2*i+200*i-1,3);
end
%% Using scattered interpolation
F = scatteredInterpolant(x',y',mag');
xq = linspace(-100,-50,100);
yq = linspace(-100,-50,100);
magOut = F(xq,yq);
%% Plotting 3-D Data
plot3(x,y,mag,'.',xq,yq,magOut,'o');
grid on
title('Linear Interpolation');
xlabel('x'), ylabel('y'), zlabel('Values');
legend('Sample data','Interpolated query data','Location','Best');
For more information regarding 'scatteredInterpolant' you can refer to the following documentation: https://www.mathworks.com/help/matlab/ref/scatteredinterpolant.html

Kategorien

Mehr zu Interpolation of 2-D Selections in 3-D Grids finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by