Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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?

Réponse acceptée

Leonardo
Leonardo le 29 Mar 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)

Plus de réponses (2)

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

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 29 Mar 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

Catégories

En savoir plus sur Graphics Objects dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by