Error using sum function in function handle
2 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
Using the following code, I get a matrix dimension error. This is caused only shen using sum inside the function.
% Data
y = [0.85 0.90]'; % Measurements array m x 1
m = length(y);
% Function
L = @(m_x,s_x) (1./s_x.^m).*exp(-0.5.*sum(((y - m_x)./s_x).^2));
% Visualization:
figure
fcontour(L,[0.6 1.4 0.05 0.15])
If I write the function analytically there is no problem.
L = @(m_x,s_x) (1./s_x.^m).*exp(-0.5.*(((y(1) - m_x)./s_x).^2 + ((y(2) - m_x)./s_x).^2));
Can anyone help me solve this issue?
7 个评论
回答(1 个)
Steven Lord
2021-2-11
I suspect you have a variable named sum in your workspace when the anonymous function is created. If I'm correct the use of the identifier sum in L will be interpreted as an attempt to index into that variable not as a call to the sum function. Rename that variable.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!