필터 지우기
필터 지우기

Error using sum function in function handle

조회 수: 2 (최근 30일)
DIMITRIS GEORGIADIS
DIMITRIS GEORGIADIS 2021년 2월 11일
댓글: DIMITRIS GEORGIADIS 2021년 2월 14일
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
DIMITRIS GEORGIADIS
DIMITRIS GEORGIADIS 2021년 2월 11일
Thank you for your time. I may try a different path
DIMITRIS GEORGIADIS
DIMITRIS GEORGIADIS 2021년 2월 14일
Turning the function L first to a symbolic expression and then using the "matlabFunction" command fixes the problem.
% Data
y = [0.85 0.90]'; % Measurements array m x 1
m = length(y);
% Function
syms m_x s_x
L = (1./s_x.^m).*exp(-0.5.*sum(((y - m_x)./s_x).^2));
g = matlabFunction(L)
% Visualization:
figure
fcontour(g,[0.6 1.4 0.05 0.15])

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

답변 (1개)

Steven Lord
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.
  댓글 수: 1
DIMITRIS GEORGIADIS
DIMITRIS GEORGIADIS 2021년 2월 11일
Thank you for your answer but this is not the case. I have not a variable named sum. The beginning of the code is exactly as stated above.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by