How to remove a single point from meshgrid?

11 ビュー (過去 30 日間)
AD
AD 2023 年 7 月 31 日
コメント済み: Star Strider 2023 年 7 月 31 日
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 日
Heyy..thanks a lot!!
Star Strider
Star Strider 2023 年 7 月 31 日
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 :-)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by