Count number of changes to 1

13 visualizzazioni (ultimi 30 giorni)
Daan
Daan il 16 Set 2015
Risposto: Image Analyst il 22 Set 2022
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.

Risposta accettata

Daan
Daan il 16 Set 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 Commento
Thorsten
Thorsten il 16 Set 2015
Just add a 0 in front of the vec
N = sum(diff([0 vec]) == 1));

Accedi per commentare.

Più risposte (5)

Image Analyst
Image Analyst il 16 Set 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 Commenti
MA-Winlab
MA-Winlab il 6 Apr 2019
Modificato: MA-Winlab il 6 Apr 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 il 7 Apr 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)

Accedi per commentare.


Nobel Mondal
Nobel Mondal il 16 Set 2015
Modificato: Nobel Mondal il 16 Set 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 il 16 Set 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 il 22 Set 2022
Spostato: Image Analyst il 22 Set 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 il 22 Set 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

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by