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

 Respuesta aceptada

Matt J
Matt J el 14 de Jul. de 2021
Editada: Matt J el 14 de Jul. de 2021
F = @(x,y) reshape( mvnpdf([x(:),y(:)]) , size(x));

5 comentarios

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.
KSSV
KSSV el 14 de Jul. de 2021
You will get good help if you share the function.
Matt J
Matt J el 14 de Jul. de 2021
Editada: Matt J el 14 de Jul. de 2021
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Etiquetas

Preguntada:

el 14 de Jul. de 2021

Comentada:

el 15 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by