"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 日

1 投票

F = @(x,y) reshape( mvnpdf([x(:),y(:)]) , size(x));

5 件のコメント

Martin Due
Martin Due 2021 年 7 月 14 日
When I run this code I still get the same warning message. Is there something else that I might have missed?
Martin Due
Martin Due 2021 年 7 月 14 日
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.
KSSV
KSSV 2021 年 7 月 14 日
You will get good help if you share the function.
Matt J
Matt J 2021 年 7 月 14 日
編集済み: Matt J 2021 年 7 月 14 日
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.
Martin Due
Martin Due 2021 年 7 月 15 日
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.

サインインしてコメントする。

その他の回答 (1 件)

カテゴリ

ヘルプ センター および File ExchangeStartup 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