How to count first identical elements without a for cycle?

1 Ansicht (letzte 30 Tage)
Mr M.
Mr M. am 21 Nov. 2018
Bearbeitet: Rik am 21 Nov. 2018
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.
  1 Kommentar
Rik
Rik am 21 Nov. 2018
You have now asked over 350 questions. Surely you have been linked to the tutorial multiple times. So you should know that you should show what you have tried so far to achieve this.
I also have a few points for clarification: Should the code measure the run length of the first run of 3s, or should it measure the length of the first run?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Rik
Rik am 21 Nov. 2018
Bearbeitet: Rik am 21 Nov. 2018
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))=[];
end
output=find(diff(data),1,'first');

Weitere Antworten (0)

Kategorien

Mehr zu Write Unit Tests finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by