필터 지우기
필터 지우기

help with this code. I keep getting error message

조회 수: 1 (최근 30일)
Folakemi Omotoye
Folakemi Omotoye 2018년 7월 31일
편집: Walter Roberson 2018년 8월 1일
when i execute the code below, i get the error message - Array indices must be positive integers or logical values.
n=30
t=0; % initialisation
for t=1:1:n %time intervals
s(t)=sum(v(t-1)); %cumulative sum of the values before current value
s(t)=sum(v(t))+s(t-1); %cumulative sum of values up to current value
g(t)=v(t) - min(v); %current value minus minimum value in the matrix from first iteration to current iteration
if s(t) > 4
disp(t) %notify the time of change
disp(v(t)) % if cumulative sum is greater than 4, display when change occurs
end
t=t+1;
end
note: v is a 1x30 vector.

답변 (1개)

Adam Danz
Adam Danz 2018년 7월 31일
편집: Adam Danz 2018년 7월 31일
If you format your code using the {} button, people can help you more easily.
The first problem I see is the line below. On the first iteration, t = 1 so v(t-1) is v(0). As the error message indicates, indices must be positive integers or logicals. Here, 0 is neither and you're requesting the 0_th value of t which causes the error.
s(t)=sum(v(t-1));
Also, there's no sense in initializing t=0 prior to your for-loop where you redefine t as 1:1:n.
  댓글 수: 15
Adam Danz
Adam Danz 2018년 8월 1일
Folakemi, when you include code in these comments, use this button to format the code.
I can't help you without knowing exactly what error message you're getting. Please cut and paste the entire error message into the comments.
Folakemi Omotoye
Folakemi Omotoye 2018년 8월 1일
it worked. Thank you so much and everyone's contributions.i really appreciate.

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by