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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by