I want to store a particular value in this loop, how can i do it please. the code is below
for t=1:1:30
s(t)=sum(v(t))+sum(v(1:t));
g(t)=v(t) - min(v(1:t));
if s(t) > 4
disp(t) %%%%%%%%line of interest
disp(v(t)) % if cumulative sum is greater than 4,note and display time of change
end
end
in the code above, the line (line of interest) displays the outcome of every iteration but i need it to only display the value of time t and the corresponding value of vector v when the threshold (4) is exceeded.

댓글 수: 4

Aquatris
Aquatris 2018년 8월 1일
Does S(t) go above multiple times? if so do you only want the first time it goes above? Share your whole code, with S and v variables, so we can have better understanding.
i want it to display only at times that the value is above the threshold. that is the whole code. {v= 1x30 matrix} and {t=1:1:30}
If that is your code, then the explanation would have to be that s(t) is greater than 4 for each of those iterations.
Did you want to know only the first time it happens? Do you want to stop calculating s when it happens, or do you want to continue calculating s but only display those values the first time it happens?
to continue calculating s but display each time it exceeds 4

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

 채택된 답변

Dennis
Dennis 2018년 8월 2일
편집: Dennis 2018년 8월 2일

0 개 추천

Does v actually contain negative values? Else every value in s will be greater 4 after it exceeds this threshold for the first time. Code below displays the first value of t and v(t) when this happens (needs adjustment if you want to change the increment of the loop).
for t=1:1:30
s(t)=sum(v(1:t));
g(t)=v(t) - min(v(1:t));
end
idx=find(s>4,1)
disp(v(idx));

댓글 수: 4

what is the significance of the 1 in your line of code {idx=find(s>4,1)}
how can i have it to be within the loop such that each time it exceeds the threshold in all iterations, it will always display or store the value at that point and record the time as well.
Dennis
Dennis 2018년 8월 2일
The 1 is the number of values greater 4 to be shown. From my understanding the loop will always exceed 4 after this point (unless there are negative values). If you omit the 1 you will get all values greater 4, however these will almost be the same as in the code you have shown.
understood.thanks for the code
Dennis
Dennis 2018년 8월 2일
편집: Dennis 2018년 8월 2일
You are welcome

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Images에 대해 자세히 알아보기

질문:

2018년 8월 1일

편집:

2018년 8월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by