load xPoints; load yPoints; j=boundary(xPoints,yPoints,0.1); Plot(xPoints(j),yPoints(j))

%How do I map the x-values to y-values here?

4 commentaires

Prasanna Routray
Prasanna Routray le 27 Sep 2024
The data points are attached here.
Hi @Prasanna Routray, it's not very clear from your question how you want to map the xPoints and yPoints data further. You've created a boundary plot for the data, and if in case, plotting the xPoints and yPoints on the same plot is what you want, you can try:
load xPoints; load yPoints; j=boundary(xPoints,yPoints,0.1);
plot(xPoints(j),yPoints(j),'LineWidth',1, 'Color','black');
hold on
plot(xPoints, yPoints);
Prasanna Routray
Prasanna Routray le 27 Sep 2024
Hi Rahul,
I want to get a set of points that map to a particular x-value.
The boundary actually forms an area.
I want to get a function or method so that whenever I get an 'x', I should be able to get a set of y-values.
Hope that make the question clear. The forum was having an issue with server so, posted it from my phone.
Image Analyst
Image Analyst le 27 Sep 2024
What if, for a given vertical line (like you specified x with some specific value), there are no y values for that exact x value? Maybe some are close but not exact. Do you want to find all y values within a certain tolerance of your specified x? If so use ismembertol().

Connectez-vous pour commenter.

 Réponse acceptée

Are you wanting all the corresponding yPoints, or just those on the boundary?
load xPoints;
load yPoints;
j=boundary(xPoints,yPoints,0.1);
To me, the simplest approach is to find the indices of the desired X value, and use that the extract the corresponding Y values.
idx = xPoints==2;
yPoints(idx)
ans = 5×1
0.0016 0.0015 0.0015 0.0014 0.0014
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
That will return all points. If you just want them from the boundary, try this.
ids = xPoints(j)==2;
yPoints(j(ids))
ans = 0.0014
Here, only one value is returned because only one X value in boundary exactly equals 2. In that case, you could use ismembertol.
LIA = ismembertol(xPoints(j),2,0.01);
yPoints(j(LIA))
ans = 2×1
0.0013 0.0014
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

9 commentaires

Prasanna Routray
Prasanna Routray le 27 Sep 2024
Hi Cris, The situation is that the boundary is filled and I have an x. How do I find the corresponding y values here? It's not only about the boundary but the vertical line segment corresponding to an x.
Cris LaPierre
Cris LaPierre le 27 Sep 2024
Please provide an example of what the result should be.
Prasanna Routray
Prasanna Routray le 27 Sep 2024
Modifié(e) : Prasanna Routray le 27 Sep 2024
Hi Cris, following an example of what I want to do.
Suppose my x=5, then the corresponding y values should lie on the vertical line as shown below.
Thanks..
There is no function for this, since by definition, a function can only have 1 Y for each X. You need 2 points to define this line. You will therefore need to come up with your own solution. Here's one way. Note that this solution is unique to this boundary shape. It may not work for other shapes.
load xPoints;
load yPoints;
j=boundary(xPoints,yPoints,0.1);
plot(xPoints(j),yPoints(j))
% Define x value
x = 5;
% Can only use interp on unique X values, so split j into increasing and
% decreasing x values
d = [0; diff(xPoints(j))];
idx1 = d>0;
idx2 = d<0;
% interpolate to find corresponding y values when increasing and decreasing
y1 = interp1(xPoints(j(idx1)),yPoints(j(idx1)),x);
y2 = interp1(xPoints(j(idx2)),yPoints(j(idx2)),x);
y = [y1,y2]
y = 1×2
0.0035 0.0045
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
hold on
plot(x*ones(length(y),1),y)
hold off
Prasanna Routray
Prasanna Routray le 30 Sep 2024
Cris, i accept this answer because we have to look for a workaround for this. This works for the timebeing as I'm fixing the boundary.
Thanks..
Cris LaPierre
Cris LaPierre le 30 Sep 2024
Feel free to submit an enhancement suggestion on the feature you would like to MathWorks here: https://www.mathworks.com/support/contact_us.html
Prasanna Routray
Prasanna Routray le 1 Nov 2024
Modifié(e) : Prasanna Routray le 1 Nov 2024
Hi Cris, What should be the way for a new shape? I mean to say that if I take a new shape more or less similar to the above one. It return NaN for y2.
My queation is that what should I be doing to avoid NaN.
I have added a different set of points for your reference.
Cris LaPierre
Cris LaPierre le 1 Nov 2024
Modifié(e) : Cris LaPierre le 1 Nov 2024
The difference approach doesn't appear to be working here. Assuming the boundary shape is always like this, consider using the location of the max x value to separate your data.
load xPointsNew;
load yPointsNew;
j=boundary(xPointsNew,yPointsNew,0.1);
plot(xPointsNew(j),yPointsNew(j))
[~,ind] = max(xPointsNew(j))
ind = 64
% Define x value
x = 5;
% Can only use interp on unique X values, so split j into increasing and
% decreasing x values
idx1 = 1:ind-1;
idx2 = ind:length(j);
% interpolate to find corresponding y values when increasing and decreasing
y1 = interp1(xPointsNew(j(idx1)),yPointsNew(j(idx1)),x);
y2 = interp1(xPointsNew(j(idx2)),yPointsNew(j(idx2)),x);
y = [y1,y2]
y = 1×2
0.0036 0.0048
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
hold on
plot(x*ones(length(y),1),y)
hold off
Hi Cris, great that you could found another way.
I too found a different way that uses polyshape and intersect function.
load xPointsNew;
load yPointsNew;
j=boundary(xPointsNew,yPointsNew,0.01);
plot(xPointsNew(j),yPointsNew(j))
pgon = polyshape(xPointsNew(j),yPointsNew(j));
x= 6;
lineseg = [x 0; x 0.01]; % [x1 y1; x2 y2]
[in,out] = intersect(pgon,lineseg);
hold on
plot(in(:,1),in(:,2),'b',out(:,1),out(:,2),'r')
xlabel( 'X [m]', 'Interpreter', 'latex', FontSize=18 );
ylabel( 'Y [m]', 'Interpreter', 'latex', FontSize=18 );
set(gca, fontsize=22, fontname='Times')
ax = gca
ax.YAxis.Exponent = 0;
axis padded
hold on
Cheers!

Connectez-vous pour commenter.

Plus de réponses (1)

Rahul
Rahul le 27 Sep 2024

0 votes

I believe that you're trying to want to obtain a reverse mapping, from xPoints data to yPoints data, using a MATLAB function. Here's how you can code the same:
function [res_x, res_y] = getYs(x, xPoints, yPoints)
x = 5;
n = size(xPoints, 1);
res_y = [];
res_x = [];
for i=1:n
if xPoints(i) == x
res_y = [res_y yPoints(i)];
res_x = [res_x x];
end
end
end
Use the above function to get yPoints values corresponding to a given 'x', plot the resultant values on the figure, and display the resultant array 'res_y':
load xPoints; load yPoints;
j=boundary(xPoints,yPoints,0.1);
plot(xPoints(j),yPoints(j), 'Color','black');
hold on;
% Call getYs to get corresponding y values for a given x = 5
x = 5;
[res_x, res_y] = getYs(x, xPoints, yPoints);
% Plot returned data using dotted red line on same graph
plot(res_x, res_y, 'r.');
disp(res_y);
hold off;

1 commentaire

Prasanna Routray
Prasanna Routray le 30 Sep 2024
Hi Rahul,
I have been trying to do reverse mapping. however, I'm looking for a set of points and not just the boundary points as I posted in one of the replays.
Thanks

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB Support Package for USB Webcams dans Centre d'aide et File Exchange

Produits

Version

R2023a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by