MATLAB Answers

Why does fplot think my function is not vectorized

조회 수: 22(최근 30일)
Matt J
Matt J 2019년 5월 30일
편집: Matt J 2019년 6월 25일
Executing the following as a script in R2018a
fun=@(x) myFourier(x,a,b,b0);
function f=myFourier(x,a,b,b0)
results successfully in a plot, but throws warnings (EDIT: and results in non-vectorized execution!!!)
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.
> In>getFunction
In fplot>singleFplot (line 234)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 193)
In fplot>vectorizeFplot (line 193)
In fplot (line 163)
In test (line 7)
But the input function is properly vectorized as is easily verified in simple tests,
>> fun(rand(1,5))
ans =
3.4371 2.5677 14.1698 2.4490 1.0828
>> fun(rand(5,1))
ans =
Why the warnings, then?

채택된 답변

Walter Roberson
Walter Roberson 2019년 5월 31일
f1 = fun(1);
f2 = fun([1 2]);
f1 - f2(1)
ans =
Your code produces different outputs for the same input, which violates the rule that the calculations must be independent.
  댓글 수: 3
Matt J
Matt J 2019년 6월 5일
Tech support got back to me and confirmed Walter's explanation of the problem, and said they will look into remedies for future releases. I passed on to them my suggestions for fixes as well.

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

추가 답변(3개)

dpb 2019년 5월 31일
Because fplot is just looking at the source code, not the output and it doesn't recognize the reshape operation at the end--only that there are no "dot" operators in the evaluation from which it infers (usually correctly, but as you've shown not infallibly) that the function isn't "vectorized".
Whether there's any chance of being able to get this one right without way more parsing logic than would want to use for performance is, I'm guessing, pretty small. You can't just presume that if the user has a reshape call in the function that always works correctly, either.
  댓글 수: 14
dpb 2019년 6월 2일
This could well be the basis for an enhancement/quality of implemenation improvement request.

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

Catalytic 2019년 6월 7일
Here's a workaround to trick fplot into doing the right thing.

Yair Altman
Yair Altman 2019년 6월 22일
As far as I can tell (never mind exactly how), internally a check is made whether the output of fun(1:3) is exactly equaln to the output of [fun(1),fun(2),fun(3)]. Even a tiny FP eps difference will cause the warning to be evoked. If you want to disable the error in run-time, run the following command:
warning off MATLAB:fplot:NotVectorized
  댓글 수: 1
Matt J
Matt J 2019년 6월 25일
Thanks, Yair. But disabling the warning still leaves the bigger problem of execution efficiency. fplot will still go ahead and evaluate fun in non-vectorized (therefore slow) fashion based on the fun(1:3) test, even with the warning disabled.

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


Community Treasure Hunt

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

Start Hunting!

Translated by