how to create a matrix when using surf function?
이전 댓글 표시
I want to solve this problem:

figure(1);
[u,v]=meshgrid(-40:1:40);
D1=sqrt((u-40)^2+v^2);
D2=sqrt((u+40)^2+v^2);
if (D1<=40)&(D2<=40)
F=0;
else
F=1;
end
surf(u,v,F);
shading interp;
however, in this way, matlab tells me ''
错误使用 surf (line 71)
Z 必须为矩阵,不能是标量或向量。''
how can I use surf correctly?
답변 (1개)
Voss
2022년 1월 18일
What you are doing there is making F a scalar, but F needs to be a matrix the same size as u and v, so something like this:
F = ones(size(u));
F(D1 <= 40 & D2 <= 40) = 0;
Here logical indexing is used to set the value of F based on conditions involving D1 and D2.
카테고리
도움말 센터 및 File Exchange에서 图形性能에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!