help with this code. I keep getting error message
Show older comments
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
Folakemi Omotoye
on 1 Aug 2018
Adam Danz
on 1 Aug 2018
Cell arrays have nothing to do with my answer. Did you understand what is causing the error?
Folakemi Omotoye
on 1 Aug 2018
Adam Danz
on 1 Aug 2018
You'll need to edit your question and format your code for me to easily read your code. Use the {} button.
Dennis
on 1 Aug 2018
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
Folakemi Omotoye
on 1 Aug 2018
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.
Folakemi Omotoye
on 1 Aug 2018
Dennis
on 1 Aug 2018
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 :)
Adam Danz
on 1 Aug 2018
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.
Folakemi Omotoye
on 1 Aug 2018
Folakemi Omotoye
on 1 Aug 2018
Dennis
on 1 Aug 2018
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)')
Adam Danz
on 1 Aug 2018
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
on 1 Aug 2018
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!