how to make a response for more than one period?

1 次查看(过去 30 天)
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 个评论
Ameer Hamza
Ameer Hamza 2020-3-9
Where are the iterations? There is no for or while loop in this code.
Ani Asoyan
Ani Asoyan 2020-3-9
This code is linked to another code in which n is the number of iterations... but I want to write a loop in this one with n so that I can make my code as I described above

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2020-3-9
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 个评论
Ani Asoyan
Ani Asoyan 2020-3-10
编辑:Ani Asoyan 2020-3-10
Yes, a little correction. at n=0 p is not zero, it is -1 ( in fact I can give it whatever I want)
when p goes to 1 at t-th iteration, function will return 'Yes' in the i-th and (i+1)-th iteration. Then the output will be 'No' as long as p has other value than 1. when p will be equal to 1 again, then again in that two iterations function will return 'Yes'
Guillaume
Guillaume 2020-3-10
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by