Accurately obtaining the value of a variable at the requested points
59 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Ayush Modi
2024 年 7 月 31 日 2:46
編集済み: Ayush Modi
2024 年 7 月 31 日 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:
回答 (1 件)
Divyam
2024 年 8 月 6 日 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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Interpolation of 2-D Selections in 3-D Grids についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!