How to extract values from specific coordinates

Dear forum,
I have a plane defined by (X,Y) coordinate points on which I interpolated a scalar, as you can see in the figure attached. How may I export the scalar only for some given (X,Y) pairs? For example X = [ -0.05 0 0.05]' , Y = [ 0.01 0.025 0.05]' and I want to get the scalar in (-0.05,0.01), (0,0.025) and (0.05,0.05).
Regards,
Robert

 Accepted Answer

Matt J
Matt J on 30 Dec 2021
Edited: Matt J on 30 Dec 2021
You can use interp2() or griddedInterpolant().

5 Comments

Thank you for the answer! I did it with griddedInterpolant() but I was wondering, is it a way to get the scalar as a column vector? In the example above I get it as a 3x3 matrix with repetitive results, so I have to extract a column or transform a row to column. My goal is to write the results into .csv or .txt file with the format:
X Y Scalar
-0.05 0.01 ...
0 0.025 ...
0.05 0.05 ...
In the example above
Where above? Ideally, you would show me the code, but you shouldn't be getting a 3x3 matrix as long as your input syntax is of the first two shown here:
Hello,
Sorry, I forgot to mention that the scalar was interpolated on a new grid using "griddata". So the Scalar being a matrix I had to create a grid from X and Y. So now, instead of getting the scalar in 3 points given by (X,Y) = (-0.05,0.01), (0,0.025),(0.05,0.05) I get it in 9 points: (-0.05,0.01), (-0.05,0.025) etc. I may extract the diagonal with the results but I guess this will be a bit memory and time consuming in the case of bigger vectors.
Regards,
Robert
[Xi,Yi]=ndgrid(xi,yi); %used this part to create a finer grid than the one I had before
Scalar = griddata(X_FirstGrid,Y_FirstGrid,Initial_Scalar,Xi,Yi);
%X_FirstGrid,Y_FirstGrid - coordinates of the first grid
X = [ -0.05 0 0.05]' , Y = [ 0.01 0.025 0.05]';
[Xr,Yr]=ndgrid(X,Y);
F=griddedInterpolant(Xr,Yr,Scalar);
%F=griddedInterpolant(X,Y,Scalar); - doesn't work, not some array length
Seeking_Scalar=F(Xr,Yr);
%Seeking_Scalar=F(X,Y); - doesn't work, not some array length
It occurs to me that you might not know what "scalar" actually means, and this is creating confusion in the discussion. A scalar is a single number, i.e., a 1x1 matrix. Your code above makes no sense if "Initial_Scalar" is a 1x1 matrix. It also makes no sense that you would expect the output of either F(Xr,Yr) or F(X,Y) to be 1x1 matrices.
It also doesn't make sense that you used griddata if your original data was already gridded.
Here is my first impression of what I think you really want. The point is that it does give a result the same size as X and Y, and griddata is not needed.
X_FirstGrid=linspace(-0.1,0.1);
Y_FirstGrid=linspace(0,0.05);
V=X_FirstGrid.^2.*Y_FirstGrid(:); %Some made-up surface
F=griddedInterpolant({X_FirstGrid, Y_FirstGrid}, V);
X = [ -0.05 0 0.05]' ; Y = [ 0.01 0.025 0.05]';
result = F(X,Y)
result = 3×1
1.0e+-3 * 0.0450 0.0000 0.3750
Hello,
Sorry for the way of expressing. Indeed this is what I was looking for!
Thank you,
Regards,
Robert

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Asked:

on 30 Dec 2021

Commented:

on 1 Jan 2022

Community Treasure Hunt

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

Start Hunting!