Interpolate pairs of values from matrix

I have a coarse 2D-matrix with [X,Y,Z]-values.
I want to input a Z-value and would like to interpolate possible values for the [X,Y]-pairs. These [X,Y]-pairs are then used for a fit. This means that for a given value of Z, if I choose any X value, Y can be calcualted and the other way arround.
I have tried the interp2 already, but it seems that I can only input X and Y values to get the interpolated Z value and not the other way around, where I input a Z-value and get a list of [X,Y]-pairs for that.
All the best
Chris

3 Comments

So essentially you want to find the coordinates on a fine sample grid where Z is close to some target value?
Can you provide example data? that will make it easier to play around with a realistic example.
Esaclty, the value for Z most likely is not listed in the original coarse matrix. This means, that the corresponding X and Y value most likely will have to be interpolated from the coarse grid of Z. Would that be possible?
To grab this example matrix, in the first colum if X=125, 0.3 is inbetween Z = 0.247 and 0.389, with Y = 4.459 4.526 respectively. Eliminating the NaN, interp1(Z(:,1),Y,0.3) would then give 4.4838. Is that the way to do it?

Sign in to comment.

 Accepted Answer

hello
you can surely get the required x , y data for a given z value ; use contour for that purpose as shown in code example below :
Z = peaks(50)/10;
level = 0.3;
surf(Z)
hold on
% extract all isoclines for a given level
[C,h] = contour(Z,level*[1 1]);
[m,n] = size(C);
ind = find(C(1,:)==level); % index of beginning of each isocline data in C
ind = [ind n+1]; % add end (+1)
for k = 1:numel(ind)-1
xc = C(1,ind(k)+1:ind(k+1)-1);
yc = C(2,ind(k)+1:ind(k+1)-1);
zc = level*ones(size(xc));
plot3(xc,yc,zc,'linewidth',5);
end
hold off

5 Comments

This worked fine, yet as you mentioned, I should add some example the data.
The solution you provided gave me the indices of the respective "X" or "Y" values. In this, for exmaple one of the given indices was 4.1111, does that mean that it is X=0.9999*X(4)+0.1111*X(5)?
The value for Z most likely is not listed in the original coarse matrix. This means, that the corresponding X and Y value most likely will have to be interpolated from the coarse grid of Z. Would that be possible too?
To grab this example matrix, in the first colum if X=125, 0.3 is inbetween Z = 0.247 and 0.389, with Y = 4.459 4.526 respectively. Eliminating the NaN, interp1(Z(:,1),Y,0.3) would then give 4.4838. Is that the way to do it?
hello again
not sure to understand what you are doing
I slightly changed the code to match with your data
so basically you end up with a 2D curve defined by xc and yc coordinates (see second plot)
you can select then any valid value for x and use interp1 to find the corresponding y value on this 2D curve
load('XYZ_test.mat')
% extract all isoclines for a given level
level = 0.3;
surf(X,Y,Z)
hold on
[C,h] = contour(X,Y,Z,level*[1 1]);
[m,n] = size(C);
ind = find(C(1,:)==level); % index of beginning of each isocline data in C
ind = [ind n+1]; % add end (+1)
for k = 1:numel(ind)-1
xc = C(1,ind(k)+1:ind(k+1)-1);
yc = C(2,ind(k)+1:ind(k+1)-1);
zc = level*ones(size(xc));
plot3(xc,yc,zc,'*-r','markersize',15);
end
hold off
% plot the xc,yc curve
figure
plot(xc,yc,'-r');
xlabel('x');
ylabel('y');
Yes, that is exactly what I tried to achieve. Thanks

Sign in to comment.

More Answers (0)

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Products

Release

R2024b

Tags

Community Treasure Hunt

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

Start Hunting!