Accurately obtaining the value of a variable at the requested points
조회 수: 2 (최근 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일
편집: Ayush Modi
2024년 7월 31일
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일
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에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!