Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [ r ] = lab4( A )
- %LAB4 counts elements between first and last positive
- %in the single-dimensional array A
- N=length(A);
- kf=-1; %index of the first positive element
- kl=-1; %index of the last positive element
- k=1; %loop counter
- while (k<=N) && (kf==-1)
- if A(k)>0
- kf=k;
- end
- k=k+1;
- end
- k=N; %downwards
- while (k>=1) && (kl==-1)
- if A(k)>0
- kl=k;
- end
- k=k-1;
- end
- if (kf==-1) && (kl==-1)
- warning('No positive elements!')
- end
- r=kl-kf-1;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement