필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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?

채택된 답변

Leonardo
Leonardo 2024년 3월 29일
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개)

VBBV
VBBV 2024년 3월 29일
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
Torsten
Torsten 2024년 3월 29일
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')

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


Steven Lord
Steven Lord 2024년 3월 29일
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

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by