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

1 Ansicht (letzte 30 Tage)
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?

Akzeptierte Antwort

Leonardo
Leonardo am 29 Mär. 2024
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)

Weitere Antworten (2)

VBBV
VBBV am 29 Mär. 2024
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 Kommentare
Torsten
Torsten am 29 Mär. 2024
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 am 29 Mär. 2024
it works now, after changing to
g = @(x,y) zeros(size(x));
graph_surface( ...
g, ...
xy_limit=2.5, ...
FaceColor='interp')

Melden Sie sich an, um zu kommentieren.


Steven Lord
Steven Lord am 29 Mär. 2024
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

Kategorien

Mehr zu Graphics Objects finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by