Count number of changes to 1

13 次查看(过去 30 天)
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.

更多回答(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 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by