How to convert 0's and 1's to intergers

For example i have an array of ones and zeros, 100 total.(00000,1111111111,0000000000000,11111...). How can i count them, and return integers for example (0,1,2,4,3,1,..)

10 Comments

what logic do you use to convert (00000,1111111111,0000000000000,11111...) to (0,1,2,4,3,1,..)?
I'm baffled too. You describe two different things (conversion from binary, and counting), then give an example that shows neither.
I had a for loop in mind to count from 1:100. For the first set of zeros, count them and assign an integer zero. For the next set of ones, count them and assign them an integer value 1 and so on...
What do you want done with the counts then?
You have 100 total entries, right? But you want to find the lengths of sequences of consecutive 0's and 1's? There wouldn't necessarily be 100 such sequences, so using a for loop from 1:100 would not seem appropriate.
Sorry for the confusion guys. This data is coming from a laser sensor and every 0 and 1 counts to a total of 100. All i am trying to do is replace(not convert) each sequence with a numerical value and end up with a short array.
What type of array is (000,111...). Are those ascii-characters? Double?
Uhhhh, do you mean you have a single stream, not a partitioned list as you described: 000001111111111000000000000011111...
And you want to mark off each low-high and high-low transition, count the values in between, and put that count into an array representing all groups?
For this data that would mean: [5,10,13,5,...]
With indices that are implicit due to the array order (and you could always subtract 1 from the 1-based index.
Can you post a concrete example of what the sequences look like and the corresponding integers that you'd want?
Geoff, thats exactly what i mean. Thanks
In the words of Hoggle from The Labyrinth.... "Well, why didn't you SAY SO?" =)

Sign in to comment.

 Accepted Answer

If that happens to be a string of characters, try:
cac = regexp( '000001111111111000000000000011111', '(0+)|(1+)', 'match' );
cellfun( @(s) numel(s), cac )
You have to check whether it starts with "1" or "0"

4 Comments

Heheh that's exactly what I would have suggested too... Except the match string can just be '(0+|1+)'.
Oh, and there's no need to make an anonymous function if you're just pumping a single input through:
cellfun( @numel, cac )
Appreciate it guys, i just happen to be new to this world of coding, thanks
I'll remember that. Thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!