help with this code. I keep getting error message

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.

Answers (1)

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 Comments

i don't want to use cell array as it will complicate the code for me to handle
Cell arrays have nothing to do with my answer. Did you understand what is causing the error?
yes. i have removed it.what i wanted to write in the line is to add the sum of previous iteration and the sum of current iteration
You'll need to edit your question and format your code for me to easily read your code. Use the {} button.
If v is a vector/matrix v(t) will only return 1 value each iteration. sum(v(t)) will be the same as v(t) since you are summing up only 1 value. If you want to sum up all values up to t in a loop you can do it like this:
for t=1:30
s(t)=sum(v(1:t));
end
n=30 v= 1x300 vector
for t=1:1:n %time intervals
s(t)=sum(v(t))+sum(v(1:t)); %cumulative sum up to current value
g(t)=v(t) - min(v); %current value minus minimum value up to current value
if s(t) > 4
disp(t) %notify the time of change
disp(v(t)) % if cumulative sum is greater than 4,note and display time of change
end
t=t+1;
plot(t,v(t))
end after implementing your corrections, the error message is that index exceeds array bounds
Cumulative sum to current value would be this:
s(t)= sum(v(1:t));
More importantly, remove this
t = t+1;
That's the purpose of the loop. You're looping through values of t already.
If that doesn't fix the error, please let us know what line is producing the error in your updated code.
thanks i have removed that but i keep getting an error. this is the new code for t=1:1:n %time intervals
s(t)=sum(v(t))+sum(v(1:t)); %cumulative sum up to current value
g(t)=v(t) - min(v(1:t)); %current value minus minimum value up to current
if s(t) > 4
disp(t) %notify the time of change
disp(v(t)) % if cumulative sum is greater than 4,note and display time of change
end
figure(1), subplot(2,1,1)
plot(t,v(t))
title('signal change plot')
xlabel('time (ms)')
ylabel('Amplitude (v)')
end
minimum value up to current value would be:
min(v(1:t))
what are you trying to plot? your plot command probably is not doing what you want :)
Folakemi, it will be easier to help you if you format your code properly so that all of your code is within the { } brackets. Also, when you have an error, tell us exactly what the error is and which line is causing the error.
how do i use the curly brackets. I am new to matlab. i have written the code below within curly brackets, i don't if this is what you mean by using the curly bracket
{for t=1:1:n %time intervals
s(t)=sum(v(t))+sum(v(1:t)); %cumulative sum up to current value
g(t)=v(t) - min(v(1:t)); %current value minus minimum value up to current value
if s(t) > 4
disp(t) %notify the time of change
disp(v(t)) % if cumulative sum is greater than 4,note and display time of change
end
plot(t,v(t))
title('signal change plot')
xlabel('time (ms)')
ylabel('Amplitude (v)')
end}
i do not know which line is giving the error. matlab did not indicate the line. it one of the reasons why im confused. it just did not plot
line {min(v(1:t)) } is to find the minimum in the vector up to the current value of t
Thats why i asked what you want to plot. plot(t,v(t)) will only plot one point and you are overwriting every plot in each loop iteration. Try:
for t=1:1:n %time intervals
s(t)=sum(v(1:t)); %cumulative sum up to current value
g(t)=v(t) - min(v(1:t)); %current value minus minimum value up to current value
if s(t) > 4
disp(t) %notify the time of change
disp(v(t)) % if cumulative sum is greater than 4,note and display time of change
end
end
plot(1:t,v(1:t))
title('signal change plot')
xlabel('time (ms)')
ylabel('Amplitude (v)')
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.
it worked. Thank you so much and everyone's contributions.i really appreciate.

Sign in to comment.

Products

Asked:

on 31 Jul 2018

Edited:

on 1 Aug 2018

Community Treasure Hunt

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

Start Hunting!