Sum in function object

조회 수: 2 (최근 30일)
Stefan Nastase
Stefan Nastase 2021년 5월 29일
편집: Star Strider 2021년 5월 29일
I am trying to write a function object for the next function:
but I can't seem to use the + operator when working with function objs.
Tried writing it like this:
loss = @(W, x, y) log(sum(W'*x)) - (y(10)*W(:,10)'*x + y(9)*W(:,9)'*x + y(8)*W(:,8)'*x + ...
y(7)*W(:,7)'*x + y(6)*W(:,6)'*x + y(5)*W(:,5)'*x+ y(4)*W(:,4)'*x + y(3)*W(:,3)'*x +y(2)*W(:,2)'*x + y(1)*W(:,1)'*x);
lossW = @(W) 0;
for i=1:n
lossW = @(W) lossW + loss(W, X(:, i), Y(i, :));
end
I need to minimize with respect to the W matrix and I can't find a way to do that.

채택된 답변

Star Strider
Star Strider 2021년 5월 29일
편집: Star Strider 2021년 5월 29일
It is not possible to operate on funciton handles themselves. It is necessary to evaluate the functions —
loss = @(W, x, y) log(sum(W'*x)) - (y(10)*W(:,10)'*x + y(9)*W(:,9)'*x + y(8)*W(:,8)'*x + ...
y(7)*W(:,7)'*x + y(6)*W(:,6)'*x + y(5)*W(:,5)'*x+ y(4)*W(:,4)'*x + y(3)*W(:,3)'*x +y(2)*W(:,2)'*x + y(1)*W(:,1)'*x);
for i=1:n
lossW = @(W) lossW(W) + loss(W, X(:, i), Y(i, :));
end
In the loop, ‘lossW’ now has an argument.
The probllem is that ‘lossW’ now re-defines and refers to itself. This is called function recursion and is to be avoided. This code will lilkely not work without some significant changes.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by