MATLAB: How to count first identical elements without a for cycle countvector I have a vector [3 3 3 3 3 4 1 1 0 0 0 5 6 3 3 1 1 1], and I want to count threes at the begining of the vector, so the result has to be 5. Best Answer In case you want to measure the run length of the first run: data=[3 3 3 3 3 4 1 1 0 0 0 5 6 3 3 1 1 1]; output=find(diff(data),1,'first');And if you want to measure the run length of the first run of 3: data=[5 3 3 3 3 3 4 1 1 0 0 0 5 6 3 3 1 1 1];ind=find(data==3,1);if ind>1 data(1:(ind-1))=[];endoutput=find(diff(data),1,'first'); Related SolutionsMATLAB: How to Speed Up matlab for cycle A)ix=([logical(0);(diff(A(1:N))~=0)]);L(find(id))=A(id);locais=L(2:end);I've got a meeting...gotta' run for now, sorry...Undoubtedly can vectorize remaining fairly much, too.One thing is it doesn't appear the for k=1:j B(k)=locais(k)'; end B=B(:);loop does anything thatB=locais;doesn't other than perhaps the column/vector orientation. I'd say create B to begin with and fix the orientation there as wanted/needed... MATLAB: Is it possible to do this without for cycle % dataY = [1 1 3 2 3 1 1 2] % M = numel(Y)K = 3% engineT = double((Y(:) - (1:K))==0) Related QuestionHow to average a data which runs on a cycleHow to set new variable for each cycleHow to do this calculation not by ‘for’ cycleHow to update an element of vector every cycleHow to make this code like a cycleHow to insert legend in plot after a for cycle
Best Answer