Count number of changes to 1

조회 수: 11 (최근 30일)
Daan
Daan 2015년 9월 16일
답변: Image Analyst 2022년 9월 22일
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.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (5개)

Image Analyst
Image Analyst 2015년 9월 16일
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
MA-Winlab
MA-Winlab 2019년 4월 6일
편집: MA-Winlab 2019년 4월 6일
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 2019년 4월 7일
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)

댓글을 달려면 로그인하십시오.


Nobel Mondal
Nobel Mondal 2015년 9월 16일
편집: Nobel Mondal 2015년 9월 16일
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 2015년 9월 16일
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 2022년 9월 22일
이동: Image Analyst 2022년 9월 22일
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 2022년 9월 22일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by