Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

#Day6- #100DaysChallenge- Matlab Loops| Occurrence of Two one in a vector

#Day6- Occurrence of Two one in a vector


Task: Find the Occurrence of two one in a given vector.

Find the number of times  [1,1] pattern occur in the  given vector

Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those

x=[1,1,1,1,3,5,6,7,12,3,4,5]

y=3

x1=[1,2,1,3,4,5,1]

y=0


Matlab Code:


         
function y = lengthOnes(x)
  n=length(x);
  y=0;
  for i=2:1:n
      if x(i)==1 && x(i-1)==1
          y=y+1;
      end
     
  end         
     
end



Sample Input and Output


>> y = lengthOnes(x)

y =

     3

>> x1=[1,2,1,2,3,4,5,1];
>> y = lengthOnes(x1)

y =

     0

>> y = lengthOnes(x2)

y =

     7

Click Here for Video Link 
      

Join us on Telegram:https://t.me/matlabirawen

Join us on Facebook Group: https://www.facebook.com/groups/matlabcodes



No comments

Popular Posts