How to remove a single point from meshgrid?

I have written this code and want to model the temperature profile. I want to exclude (0,0) from the meshgrid as the function becomes infinite there. How can I do that?
x = linspace(-3,10);
y = linspace(-3,0);
zero_x_idx = find(x == 0);
zero_y_idx = find(y == 0);
% Exclude the point (0, 0) from the arrays
x = x([1:zero_x_idx-1, zero_x_idx+1:end]);
y = y([1:zero_y_idx-1, zero_y_idx+1:end]);
[X,Y] = meshgrid(x,y);
Z=(P_l*exp(-v(sqrt((X.*10^(-3)).^2 + (Y.*10^(-3)).^2) + (X.*10^(-3)))/(2*a)))/(4*3.14*k*sqrt((X.*10^(-3)).^2 + (Y.*10^(-3)).^2))+T0;

댓글 수: 1

Matt J
Matt J 2023년 7월 31일
편집: Matt J 2023년 7월 31일
Exclude it for what purpose? If you're just plotting, it doesn't matter if you leave it in there.

댓글을 달려면 로그인하십시오.

답변 (2개)

Star Strider
Star Strider 2023년 7월 31일
I would use ‘logical indexing’ to simply delete that point —
x = linspace(-3,10)
x = 1×100
-3.0000 -2.8687 -2.7374 -2.6061 -2.4747 -2.3434 -2.2121 -2.0808 -1.9495 -1.8182 -1.6869 -1.5556 -1.4242 -1.2929 -1.1616 -1.0303 -0.8990 -0.7677 -0.6364 -0.5051 -0.3737 -0.2424 -0.1111 0.0202 0.1515 0.2828 0.4141 0.5455 0.6768 0.8081
y = linspace(-3,0)
y = 1×100
-3.0000 -2.9697 -2.9394 -2.9091 -2.8788 -2.8485 -2.8182 -2.7879 -2.7576 -2.7273 -2.6970 -2.6667 -2.6364 -2.6061 -2.5758 -2.5455 -2.5152 -2.4848 -2.4545 -2.4242 -2.3939 -2.3636 -2.3333 -2.3030 -2.2727 -2.2424 -2.2121 -2.1818 -2.1515 -2.1212
x = x(x~=0)
x = 1×100
-3.0000 -2.8687 -2.7374 -2.6061 -2.4747 -2.3434 -2.2121 -2.0808 -1.9495 -1.8182 -1.6869 -1.5556 -1.4242 -1.2929 -1.1616 -1.0303 -0.8990 -0.7677 -0.6364 -0.5051 -0.3737 -0.2424 -0.1111 0.0202 0.1515 0.2828 0.4141 0.5455 0.6768 0.8081
y = y(y~=0)
y = 1×99
-3.0000 -2.9697 -2.9394 -2.9091 -2.8788 -2.8485 -2.8182 -2.7879 -2.7576 -2.7273 -2.6970 -2.6667 -2.6364 -2.6061 -2.5758 -2.5455 -2.5152 -2.4848 -2.4545 -2.4242 -2.3939 -2.3636 -2.3333 -2.3030 -2.2727 -2.2424 -2.2121 -2.1818 -2.1515 -2.1212
Another option would be to replace it with something small, however not zero —
x = linspace(-3,10)
x = 1×100
-3.0000 -2.8687 -2.7374 -2.6061 -2.4747 -2.3434 -2.2121 -2.0808 -1.9495 -1.8182 -1.6869 -1.5556 -1.4242 -1.2929 -1.1616 -1.0303 -0.8990 -0.7677 -0.6364 -0.5051 -0.3737 -0.2424 -0.1111 0.0202 0.1515 0.2828 0.4141 0.5455 0.6768 0.8081
y = linspace(-3,0)
y = 1×100
-3.0000 -2.9697 -2.9394 -2.9091 -2.8788 -2.8485 -2.8182 -2.7879 -2.7576 -2.7273 -2.6970 -2.6667 -2.6364 -2.6061 -2.5758 -2.5455 -2.5152 -2.4848 -2.4545 -2.4242 -2.3939 -2.3636 -2.3333 -2.3030 -2.2727 -2.2424 -2.2121 -2.1818 -2.1515 -2.1212
x(x==0) = NaN
x = 1×100
-3.0000 -2.8687 -2.7374 -2.6061 -2.4747 -2.3434 -2.2121 -2.0808 -1.9495 -1.8182 -1.6869 -1.5556 -1.4242 -1.2929 -1.1616 -1.0303 -0.8990 -0.7677 -0.6364 -0.5051 -0.3737 -0.2424 -0.1111 0.0202 0.1515 0.2828 0.4141 0.5455 0.6768 0.8081
y(y==0) = NaN
y = 1×100
-3.0000 -2.9697 -2.9394 -2.9091 -2.8788 -2.8485 -2.8182 -2.7879 -2.7576 -2.7273 -2.6970 -2.6667 -2.6364 -2.6061 -2.5758 -2.5455 -2.5152 -2.4848 -2.4545 -2.4242 -2.3939 -2.3636 -2.3333 -2.3030 -2.2727 -2.2424 -2.2121 -2.1818 -2.1515 -2.1212
x = fillmissing(x, 'nearest')
x = 1×100
-3.0000 -2.8687 -2.7374 -2.6061 -2.4747 -2.3434 -2.2121 -2.0808 -1.9495 -1.8182 -1.6869 -1.5556 -1.4242 -1.2929 -1.1616 -1.0303 -0.8990 -0.7677 -0.6364 -0.5051 -0.3737 -0.2424 -0.1111 0.0202 0.1515 0.2828 0.4141 0.5455 0.6768 0.8081
y = fillmissing(y, 'nearest')
y = 1×100
-3.0000 -2.9697 -2.9394 -2.9091 -2.8788 -2.8485 -2.8182 -2.7879 -2.7576 -2.7273 -2.6970 -2.6667 -2.6364 -2.6061 -2.5758 -2.5455 -2.5152 -2.4848 -2.4545 -2.4242 -2.3939 -2.3636 -2.3333 -2.3030 -2.2727 -2.2424 -2.2121 -2.1818 -2.1515 -2.1212
The second approach sets the zero value equal to NaN, and then interpolates it with the nearest (most likely non-zero) value.
.

댓글 수: 4

AD
AD 2023년 7월 31일
Thanks! I am also getting this error: Array indices must be positive integers or logical values.
Error in temp_profile (line 16)
Z=(P_l*exp(-v(sqrt((X*10^(-3)).^2 + (Y*10^(-3)).^2) + (X.*10^(-3)))/(2*a)))/(4*3.14*k*sqrt((X*10^(-3)).^2 + (Y*10^(-3)).^2))+T0;
The problem is here:
Z=(P_l*exp(-v(sqrt((X*10^(-3)).^2 + (Y*10^(-3)).^2) + (X.*10^(-3)))/(2*a)))/(4*3.14*k*sqrt((X*10^(-3)).^2 + (Y*10^(-3)).^2))+T0;
HERE
It needs to be:
Z=(P_l*exp(-v.*(sqrt((X.*10^(-3)).^2 + (Y.*10^(-3)).^2) + (X.*10^(-3)))./(2*a)))./(4*pi*k*sqrt((X.*10^(-3)).^2 + (Y.*10^(-3)).^2))+T0;
instead.
There is a missing operator of some type (I assume multiplication). I also did element-wise division everywhere, sincer that is usually overlooked and I assume that here you would want to do it.
x = linspace(-3,10);
y = linspace(-3,0);
x(x==0) = NaN;
y(y==0) = NaN;
x = fillmissing(x, 'nearest');
y = fillmissing(y, 'nearest');
[X,Y] = meshgrid(x,y);
P_l = randn;
v = randn;
a = randn;
k = randi(10);
T0 = randn;
Z=(P_l*exp(-v.*(sqrt((X.*10^(-3)).^2 + (Y.*10^(-3)).^2) + (X.*10^(-3)))./(2*a)))./(4*pi*k*sqrt((X.*10^(-3)).^2 + (Y.*10^(-3)).^2))+T0;
figure
contourf(X, Y, Z)
That worked correctly when I ran it.
EDIT — (31 Jul 2023 at 17:21)
Added contour plot (as contourf) since that was requested.
.
AD
AD 2023년 7월 31일
Heyy..thanks a lot!!
My pleasure!

댓글을 달려면 로그인하십시오.

Voss
Voss 2023년 7월 31일
First, notice that zero doesn't appear in x:
x = linspace(-3,10);
zero_x_idx = find(x == 0);
zero_x_idx
zero_x_idx = 1×0 empty double row vector
But if you used different endpoints or a different number of points in linspace, zero might appear:
x = linspace(-3,10,131);
zero_x_idx = find(x == 0);
zero_x_idx
zero_x_idx = 31
So, I'm going to go with that.
(Zero does appear in y):
y = linspace(-3,0);
zero_y_idx = find(y == 0);
zero_y_idx
zero_y_idx = 100
Second, notice that removing the 0 from x and the 0 from y before using them in meshgrid removes not only the point (0,0) but also the lines x=0 and y=0 from the X and Y matrices returned by meshgrid. In other words, you'll have the situation where X and Y don't have any points where X is 0 or Y is 0, instead of the situation where X and Y don't have any points where X is 0 and Y is 0. (I'm sure you've noticed this already but were not sure how to remove a single point instead of both lines, hence the question.)
Here's one way to remove the single point (0,0) from X and Y:
[X,Y] = meshgrid(x,y);
% note: X,Y includes (0,0) at this point in the code:
[r0,c0] = find(X == 0 & Y == 0)
r0 = 100
c0 = 31
% create a logical matrix of which points to keep:
to_keep = true(size(X));
% set the element at zero_y_idx,zero_x_idx to false:
to_keep(zero_y_idx,zero_x_idx) = false;
% only keep the points in X and Y where to_keep is true:
X = X(to_keep);
Y = Y(to_keep);
% sanity check: that (0,0) is gone
[r0,c0] = find(X == 0 & Y == 0)
r0 = 0×1 empty double column vector c0 = 0×1 empty double column vector

댓글 수: 2

AD
AD 2023년 7월 31일
Thanks! I want to make a contour plot of the z function. However, I am getting only a single value of Z (300) for all values in the mesh with no countour plots. What is the error?
Torsten
Torsten 2023년 7월 31일
Maybe we could tell, but - as you can imagine - it's difficult without your code :-)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

AD
2023년 7월 31일

댓글:

2023년 7월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by