Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

traverse the list of array elements

Asked by pammy

pammy (view profile)

on 13 Mar 2013

i've code the following for traversing

 function traverse
  clc;
  a=[ 1 2 2 3 3 4 4 4 4]
  for i=1:length(a)
    if(a(i)==a(i+1))
        fprintf('\nmatch\n %d and %d',a(i),a(i+1));
    else
        fprintf('\nnot a match\n');
    end
end

but the following error occurs

      Attempted to access a(10); index out of bounds because numel(a)=9.

tell me the way to sort it out...

0 Comments

pammy

pammy (view profile)

Products

No products are associated with this question.

1 Answer

Answer by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 13 Mar 2013
Accepted answer

Use

for i=1:length(a)-1

1 Comment

pammy

pammy (view profile)

on 13 Mar 2013

thank u

Azzi Abdelmalek

Azzi Abdelmalek (view profile)

Contact us