Anonymous Functions - how to store the results of anonymous functions

조회 수: 5 (최근 30일)
Ken
Ken 2022년 4월 6일
댓글: Ken 2022년 4월 6일
rng(1) % important for technical reasons
U = [ 1 -1 3 4 ]; % an artificial input sequence
% applyPredictionUpdate returns the predicted state:
applyPredictionUpdate = @(bel, u_t) [bel '.PU(' num2str(u_t) ')'];
% readSensor returns the current sensor's reading:
readSensor = @() [ 'Z(' num2str(round(20*rand())) ')' ];
% applyMeasurementUpdate returns the state updated by the measurements:
applyMeasurementUpdate = @(sensorReading, belBar) [ belBar '.MU(' sensorReading ')' ];
bel = 'PRIOR';
%%%%%%%%%%% PLEASE DON'T CHANGE ANYTHING ABOVE THIS LINE %%%%%%%%%%%
for u_t = U
belBar=@(bel, u_t)[bel,u_t];
sensorReading=readSensor();
bel=applyMeasurementUpdate(sensorReading,belBar);
end
Got the foll. error:
Error using horzcat
The following error occurred converting from char to function_handle:
Too many output arguments.
Error in solution>@(sensorReading,belBar)[belBar,'.MU(',sensorReading,')'] (line 8)
applyMeasurementUpdate = @(sensorReading, belBar) [ belBar '.MU(' sensorReading ')' ];
Error in solution (line 14)
bel=applyMeasurementUpdate(sensorReading,belBar);
" applyPredictionUpdate does not change bel in-place but returns the predicted state, so make sure to store it in a local variable that you can use in applyMeasurementUpdate."
  댓글 수: 1
Stephen23
Stephen23 2022년 4월 6일
As far as I can tell, defining belBar as a function handle is a red-herring.
rng(1) % important for technical reasons
U = [ 1 -1 3 4 ]; % an artificial input sequence
% applyPredictionUpdate returns the predicted state:
applyPredictionUpdate = @(bel, u_t) [bel '.PU(' num2str(u_t) ')'];
% readSensor returns the current sensor's reading:
readSensor = @() [ 'Z(' num2str(round(20*rand())) ')' ];
% applyMeasurementUpdate returns the state updated by the measurements:
applyMeasurementUpdate = @(sensorReading, belBar) [ belBar '.MU(' sensorReading ')' ];
bel = 'PRIOR';
%%%%%%%%%%% PLEASE DON'T CHANGE ANYTHING ABOVE THIS LINE %%%%%%%%%%%
for u_t = U
sensorReading=readSensor();
bel=applyMeasurementUpdate(sensorReading,[bel,u_t])
end
bel = 'PRIOR.MU(Z(8))'
bel = 'PRIOR.MU(Z(8)) .MU(Z(14))'
bel = 'PRIOR.MU(Z(8)) .MU(Z(14)).MU(Z(0))'
bel = 'PRIOR.MU(Z(8)) .MU(Z(14)).MU(Z(0)).MU(Z(6))'
But the best solution would be to not write code that writes code like that.

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

채택된 답변

David Hill
David Hill 2022년 4월 6일
for u_t = U
belBar=applyPredictionUpdate(bel, u_t);
sensorReading=readSensor();
bel=applyMeasurementUpdate(sensorReading,belBar);
end

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 4월 6일
belBar=@(bel, u_t)[bel,u_t];
That is a function handle
applyMeasurementUpdate = @(sensorReading, belBar) [ belBar '.MU(' sensorReading ')' ];
You are requesting to concatenate the function handle and a character vector.
belBar is a function expecting two inputs, but you are trying to wrap it in a function that takes inputs that do not include the two inputs belBar needs, so you are going to have trouble.
I think it might make more sense to use
belBar = [bel,u_t];

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by