how to make a response for more than one period?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
function response = f(n,p)
if (p == 1)
response = 'yes';
elseif (p == 2)
response = 'no';
end
This is my function.
n is the number of iterations.
I want to make a response with memory. For example
if (p==1), response will be 'yes' for 2 iterations, then it will be 'no' regardless what p would be
2 commentaires
Réponse acceptée
Guillaume
le 9 Mar 2020
Your question is still very unclear. I think you're saying that in another function you've got a loop that calls your function passing p and n, the latter being the loop index. In which case, it's as simple as:
function response = f(p, n) %much better function name required! Use full words that describe what the function does
if p == 1 & n < 3
response = 'yes';
else %happens when p is not 1 or from the 3rd iteration
response = 'no';
end
end
A few notes about the code you've written:
- in matlab you don't need to enclose an if condition in (). Personally, I find that wrapping if condition in () make the code harder to read.
- Never end an if on an elseif. The code you wrote would error (with "output response not assigned") if p is anything other than the scalar 1 or 2. At least my code (which ends with an else not an elseif) will return 'no'.
- Use a better name for your function and write comment. Writing code that can be understood by others is just as important as writing code that works.
6 commentaires
Guillaume
le 10 Mar 2020
Ok, you just want to implement a latch for two cycles. This requires the function to have memory of a state between calls. That's not typical but can be implemented with persistent variables. However, if you want to keep things simple, particularly as the function does so little, you may be better off implementing the latching directly in your loop:
%... main code that does the looping
latch_countdown = 0; %default state when not latched
latch_duration = 2; %duration of latch in loop iterations. Includes the iteration when it is set. Hence 2 is n and n+1
for n = 0:whatever %could be a while loop as well. Doesn't matter
%... code that sets p
if p == 1
latch_countdown = latch_duration; %start latch
end
if latch_countdown > 0
latch_countdown = latch_countdown - 1;
response = 'yes';
else
response = 'no';
end
%...
end
Now, if you do want to do the same in a function, as said you have to use a persistent variable:
function response = latched_response(p, latch_duration)
%latch for latch_duration iterations (n), when p is 1.
%latch_duration is optional, default value is 2.
persistent latch_countdown;
if nargin < 3
latch_duration = 2; %default value if not specified
end
if isempty(latch_countdown) %first call to the function
latch_countdown = 0;
end
if p == 1
latch_countdown = latch_duration; %start latch
end
if latch_countdown > 0
latch_countdown = latch_countdown - 1;
response = 'yes';
else
response = 'no';
end
end
with your main code being more or less:
latch_duration = 2; %duration of latch in loop iterations. Includes the iteration when it is set. Hence 2 is n and n+1
for n = 0:whatever %could be a while loop as well. Doesn't matter
%... code that sets p
response = latched_response(p, latch_duration);
%...
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Multidimensional Arrays 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!