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

 Accepted Answer

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

5 Comments

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.

Sign in to comment.

Categories

Products

Release

R2021a

Tags

Asked:

on 14 Jul 2021

Commented:

on 15 Jul 2021

Community Treasure Hunt

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

Start Hunting!