Not getting the expected size matrix from evaluating a function handle that is equal to zero

h = @(x,y) 0
[X, Y] = meshgrid(linspace(0, 2, 10));
Z = h(X,Y);
X and Y are 10 x 10.
I am expecting Z to be zeros matrix of 10x10. But I am getting Z to be a 0 of 1x1.
Why?

 採用された回答

h = @(x,y) zeros(size(x));
[X, Y] = meshgrid(linspace(0, 2, 10));
Z = h(X,Y)
Z = 10×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
surf(X,Y,Z)

その他の回答 (2 件)

h = @(x,y) zeros(10)
h = function_handle with value:
@(x,y)zeros(10)
[X, Y] = meshgrid(linspace(0, 2, 10));
Z = h(X,Y)
Z = 10×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

11 件のコメント

Nilay Modi
Nilay Modi 2024 年 3 月 29 日
But I am not going to know ahead of time what my meshgrid output size will be...I am trying to have a general function.
Replace 10 by size(x):
h = @(x,y) zeros(size(x));
[X, Y] = meshgrid(linspace(0, 2, 10));
Z = h(X,Y)
Z = 10×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Nilay Modi
Nilay Modi 2024 年 3 月 29 日
something like this...
function graph_surface(h, NameValueArgs)
arguments
h function_handle
NameValueArgs.xy_limit
end
[X, Y] = meshgrid(linspace(0, NameValueArgs.xy_limit, 10));
Z = h(X,Y);
surf(X,Y,Z)
end
Torsten
Torsten 2024 年 3 月 29 日
編集済み: Torsten 2024 年 3 月 29 日
Then you will have to pass this information as input to the function handle.
You could do
h = @(x,y) zeros(size(x))
or more general
h = @(x,y,m,n) zeros(m,n);
[X, Y] = meshgrid(linspace(0, 2, 10));
Z = h(X,Y,10,10)
Z = 10×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Nilay Modi
Nilay Modi 2024 年 3 月 29 日
Ok. I see your work arounds.
But why isn't Matlab not matching my intuition...for every input (x,y) I am expecting f(x,y) = 0...so I expect z to be zeros of the size of my input matrix and instead Matlab spits out one 0...and then we go about finguring out work arounds.
Torsten
Torsten 2024 年 3 月 29 日
編集済み: Torsten 2024 年 3 月 29 日
And what would you expect MATLAB to return if x and y have different sizes ? Should it read your mind ?
You must explicitly tell MATLAB the size of the matrix of zeros it should return. In your case
h = @(x,y) 0
you told MATLAB to return the scalar 0 - independent of the sizes of the inputs.
Bruno Luong
Bruno Luong 2024 年 3 月 29 日
編集済み: Bruno Luong 2024 年 3 月 29 日
Believe or not MATLAB is not yet smater than you. It does exacly what you tell it to do (returning scalar 0)
Nilay Modi
Nilay Modi 2024 年 3 月 29 日
syms g(x,y)
g(x,y) = zeros(size(x));
graph_surface( ...
matlabFunction(g), ...
xy_limit=5, ...
FaceColor='interp')
function graph_surface(h, NameValueArgs)
arguments
h function_handle
NameValueArgs.xy_limit
NameValueArgs.FaceColor
end
[X, Y] = meshgrid(linspace(0,NameValueArgs.xy_limit,10));
Z = h(X,Y);
surf(X,Y,Z, FaceColor=NameValueArgs.FaceColor)
xlabel('x')
ylabel('y')
zlabel('z')
box on
ax = gca;
ax.BoxStyle = 'full';
axis tight
axis equal
axis vis3d
end
Nilay Modi
Nilay Modi 2024 年 3 月 29 日
inside my graph_surface function, h is just 0 and not zeros(size(x)). Why?
Define
g = @(x,y,m,n) zeros(m,n)
in the script part
and use it as
Z = g(X,Y,size(X,1),size(X,2))
in your function.
Don't overcomplicate things - your code looks complicated enough.
Nilay Modi
Nilay Modi 2024 年 3 月 29 日
it works now, after changing to
g = @(x,y) zeros(size(x));
graph_surface( ...
g, ...
xy_limit=2.5, ...
FaceColor='interp')

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

I am expecting Z to be zeros matrix of 10x10. But I am getting Z to be a 0 of 1x1.
Why?
Because that's what you told your function to return.
Based on your later comment, you don't want your function to return a 1-by-1 or even a fixed 10-by-10. You want it to return a zeros matrix the same size as the input. Correct?
f = @(X, Y) zeros(size(X));
X = ones(4)
X = 4×4
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
f(X, X)
ans = 4×4
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Y = ones(5, 8)
Y = 5×8
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
f(Y, Y)
ans = 5×8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Z = 42
Z = 42
f(Z, Z)
ans = 0

カテゴリ

製品

リリース

R2022a

質問済み:

2024 年 3 月 29 日

コメント済み:

2024 年 3 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by