index out of bounds because numel(a)=7, please help?

Hi, I'm afraid I'm new on Matlab and I don't understand what this error message means. I am trying to write a program which counts a streak of ones but I am quite bad at programming. I keep receiving this error message:
"Attempted to access a(8); index out of bounds because numel(a)=7.
Error in counthead (line 8)
if a(k+1)>0;
My code so far:
count=0;
for k=10
a(k)=round(rand);
if (a(k)>0);
count=count+1;
if a(k+1)>0;
count=count+1;
elseif a(k+1)<1;
count=0;
end
else
count=0;
end
end
What is going wrong? How could I fix it? If anyone could help I'd be really grateful. Thank you!

Answers (2)

Adam
Adam on 10 Feb 2015
Edited: Adam on 10 Feb 2015
Given the code I am surprised you reached a(8) before it crashed! Incidentally is that the exact code you ran?
for k = 10
does not create a loop at all so k would never be 7 in that code unless you actually have
for k = 1:10
Your code is putting numbers into the array a one at a time, but is trying to access the next ( k+1 ) value in the array each iteration even though it has not yet reached that index to put a value into a.
e.g. when k = 3 you assign a value to a(3) but then you try to access a(4) which does not yet exist until the next time round the loop.
What is the aim of that piece of code?
You can create the array of all 10 values upfront before the for loop:
a = round( rand(1,10) );
for k = 1:10
...
end
then do your checks on that code just missing out the
a(k) = round(rand)
line.
I haven't checked that that is the only problem in your loop, but it is the one causing that error.
round( rand ) is probably not the best way to just create an array of random 0s and 1s, but it'll do for the purposes of what you are doing I'm sure.

3 Comments

Hi Adam, Thanks for your reply. It was the code I ran, apologies I know it's a really bad piece of code. I want to write a program which counts the maximum streak of 1s produced by the equality a. I have modified the code to:
c=0;
a=round(rand(1,10));
if a>0
c=c+1;
end
disp(c)
which I know is intended to count the total number of 1s produced, but unfortunately it''s still not working. Could you possibly give me a hint as to what's going wrong?
nnz( a );
will give you the number of non-zero values in the array a.
That will not give you the longest consecutive run of 1s though of course. That would need some extra code, either a loop around a or there may be some way to do it without a loop.
Thanks very much I will give it a go!

Sign in to comment.

"I want to write a program which counts the maximum streak of 1s": this can be done very easily without a loop, using completely vectorized code:
A = round(rand(1,10));
X = diff(A);
Y = 1 + find([X<0,A(end)]) - find([A(1),X>0])
max(Y)
The vector Y gives the lengths of each sequence of 1's in the vector A, you can then pick the longest with using a simple max operation. The code is also fairly robust: it works even if there are no ones OR no zeros, although A cannot be empty.

2 Comments

Thank you! This is working great!
I'm glad to be able to help. It is also considered polite if you Accept an answer that helped solve your problem.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 10 Feb 2015

Edited:

on 17 Feb 2015

Community Treasure Hunt

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

Start Hunting!