"Properly vectorize your function"-warning. fcontour & mvnpdf

F = @(x,y) mvnpdf([x,y]);
fc = fcontour(F);
This code generates the following warning:
Warning: Function behaves unexpectedly on array inputs. To improve performance,
properly vectorize your function to return an output with the same size and shape as
the input arguments.
What should I do to get rid of this warning and increase performance?

 채택된 답변

Matt J
Matt J 2021년 7월 14일
편집: Matt J 2021년 7월 14일
F = @(x,y) reshape( mvnpdf([x(:),y(:)]) , size(x));

댓글 수: 5

When I run this code I still get the same warning message. Is there something else that I might have missed?
The suggestion in your edit removes the warning but takes about twice as much time to run as the original code. Thank you for your time nonetheless.
You will get good help if you share the function.
Thank you for your time nonetheless.
You're quite welcome, but please Accept-click the answer to indicate that your question was resolved.
The suggestion in your edit removes the warning but takes about twice as much time to run as the original code.
That's definitely not what the test below shows us:
hfig=figure('Visible','off');
F = @(x,y) mvnpdf([x,y]);
tic
fc = fcontour(F);
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
hfig.Visible='on';
toc
Elapsed time is 0.479005 seconds.
hfig.Visible='off';
F = @(x,y) reshape( mvnpdf([x(:),y(:)]) , size(x));
tic
fc = fcontour(F);
hfig.Visible='on';
toc
Elapsed time is 0.127763 seconds.
There might have been some caching at play during my testing, or I simply made a mistake. Thank you for the help! A slight variation of this code snippet runs for thousands of times in my program so the improvement means a lot.

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

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

질문:

2021년 7월 14일

댓글:

2021년 7월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by