Effacer les filtres
Effacer les filtres

Count number of changes to 1

12 vues (au cours des 30 derniers jours)
Daan
Daan le 16 Sep 2015
Hi, I have got a binary vector which looks like this: vec = [0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 1 1 1 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 ] I would like to know how many times it changes to 1. In this case six times.
I have tried it by first counting the number of changes: r=sum(abs(diff(vec))) and then devide it by 2. This will work when I have got an equal number of changes to 1 and changes to 0, but this is unfortunately not always the case.
What is the best way to do solve this in matlab?
Many thanks.

Réponse acceptée

Daan
Daan le 16 Sep 2015
Thank you both, but I still have one problem. I have also got binary vectors which start with a 1 like: v= [1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 0] and I want to get eventually the number of streaks of the number 1 (in this case 3). When I determine the number of changes the first streak of 1's isn't counted. How can I solve this problem?
Thanks in advance.
  1 commentaire
Thorsten
Thorsten le 16 Sep 2015
Just add a 0 in front of the vec
N = sum(diff([0 vec]) == 1));

Connectez-vous pour commenter.

Plus de réponses (5)

Image Analyst
Image Analyst le 16 Sep 2015
Get rid of the abs(). Just simply do this:
vec = [0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 1 1 1 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 ]
d = diff(vec)
numChangesTo1 = sum(d == 1)
  2 commentaires
MA-Winlab
MA-Winlab le 6 Avr 2019
Modifié(e) : MA-Winlab le 6 Avr 2019
In my case, if I have similar vector with blocks of 0s and blocks of 1s, I want to find the index(s) at which a change from 1 to 0 happens(store them in a vector) and the index(s) at which a change from 0 to 1 happens (returing them in a seprate vector).
Your comments are appreciated
UPDATE:
SW =[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1]
I used ischange, but it only returns 1s in the places for both: changes when there is change from 1 to 0 and similarly, when there is change from 0 to 1. In this case, I am not able to know if the change was from 0 to 1 or from 1 to 0, I only know that there was a change.
MA-Winlab
MA-Winlab le 7 Avr 2019
Update
I figured out how to do that (in my case, the vector of 0s and 1s has an even number):
OpenClose = find(diff(sign(A))) + 1;
OneToZero = OpenClose(1:2:end)
ZeroToOne = OpenClose(2:2:end)

Connectez-vous pour commenter.


Nobel Mondal
Nobel Mondal le 16 Sep 2015
Modifié(e) : Nobel Mondal le 16 Sep 2015
If you're only counting the changes 0 -> 1 , then it would be the number of occurrences of 1 (1-0) in the diff vector.
>> A = [0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 1 1 1 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 ];
>> count = length(find(diff(A) == 1));
Your code is actually counting the overall number of value changes in the vector. Hope this helps.

Image Analyst
Image Analyst le 16 Sep 2015
Daan:
If you want the number of streaks in a vector like v= [1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0 0], then, if you have the Image Processing Toolbox, simply do this:
[L, numberOfStreaks] = bwlabel(v);
numberOfStreaks will be 3 since you have 3 separate sections of 1's. Also, each streak will have a unique ID number, given in the L vector, in case you need to use that.

Mitchell Evan
Mitchell Evan le 22 Sep 2022
Déplacé(e) : Image Analyst le 22 Sep 2022
Hey, So It looks like I'm pretty late to the game and my solution is pretty hacky but it works!
I had a similar situation where I needed to count the numer of times a signal went from 1 to -1.
count("1 -1",string(sign(vec)))
%so for you count
("0 1",string(sign(Zeroed')))
It counts the number of occurances of that input pattern string.
String methods for the win!

Image Analyst
Image Analyst le 22 Sep 2022
By far the simplest answer is to simply use strfind. It's a single line of code. It's a little known trick that strfind works for numerical arrays as well as character arrays:
vec = [0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 1 1 1 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 ];
numChangesTo1 = numel(strfind(vec, [0, 1])) % Find and count 0-to-1 sequences.
numChangesTo1 = 6

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by