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 comentarios

Thomas
Thomas el 2 de Mayo de 2012
what logic do you use to convert (00000,1111111111,0000000000000,11111...) to (0,1,2,4,3,1,..)?
Geoff
Geoff el 2 de Mayo de 2012
I'm baffled too. You describe two different things (conversion from binary, and counting), then give an example that shows neither.
kimiri
kimiri el 2 de Mayo de 2012
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...
Walter Roberson
Walter Roberson el 2 de Mayo de 2012
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.
kimiri
kimiri el 2 de Mayo de 2012
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.
per isakson
per isakson el 2 de Mayo de 2012
What type of array is (000,111...). Are those ascii-characters? Double?
Geoff
Geoff el 2 de Mayo de 2012
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.
Richard Brown
Richard Brown el 2 de Mayo de 2012
Can you post a concrete example of what the sequences look like and the corresponding integers that you'd want?
kimiri
kimiri el 2 de Mayo de 2012
Geoff, thats exactly what i mean. Thanks
Geoff
Geoff el 2 de Mayo de 2012
In the words of Hoggle from The Labyrinth.... "Well, why didn't you SAY SO?" =)

Iniciar sesión para comentar.

 Respuesta aceptada

per isakson
per isakson el 2 de Mayo de 2012
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 comentarios

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Preguntada:

el 2 de Mayo de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by