Anonymous Functions - how to store the results of anonymous functions
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Stephen23
le 6 Avr 2022
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.
Réponse acceptée
David Hill
le 6 Avr 2022
for u_t = U
belBar=applyPredictionUpdate(bel, u_t);
sensorReading=readSensor();
bel=applyMeasurementUpdate(sensorReading,belBar);
end
Plus de réponses (1)
Walter Roberson
le 6 Avr 2022
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 commentaires
Voir également
Catégories
En savoir plus sur Handle Classes dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!