Anonymous Functions - how to store the results of anonymous functions
1 回表示 (過去 30 日間)
古いコメントを表示
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
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
But the best solution would be to not write code that writes code like that.
採用された回答
David Hill
2022 年 4 月 6 日
for u_t = U
belBar=applyPredictionUpdate(bel, u_t);
sensorReading=readSensor();
bel=applyMeasurementUpdate(sensorReading,belBar);
end
その他の回答 (1 件)
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];
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Handle Classes についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!