Advertisement
ALTracer

Lab5v12

Nov 16th, 2016
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.49 KB | None | 0 0
  1. function [ r ] = lab4( A )
  2. %LAB4 counts elements between first and last positive
  3. %in the single-dimensional array A
  4. N=length(A);
  5. kf=-1; %index of the first positive element
  6. kl=-1; %index of the  last positive element
  7.  
  8. k=1; %loop counter
  9. while (k<=N) && (kf==-1)
  10.   if A(k)>0
  11.       kf=k;
  12.   end
  13.   k=k+1;
  14. end
  15.  
  16. k=N; %downwards
  17. while (k>=1) && (kl==-1)
  18.   if A(k)>0
  19.       kl=k;
  20.   end
  21.   k=k-1;
  22. end
  23.  
  24. if (kf==-1) && (kl==-1)
  25.     warning('No positive elements!')
  26. end
  27. r=kl-kf-1;
  28. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement