Hi,
This is what i want... I have a binary array
001111000000011100000000011111
from here i have to count the number 1 in such way
result: 0,4,0,3,0,5.... how to get this?

 Réponse acceptée

Image Analyst
Image Analyst le 22 Juil 2014

3 votes

If you have the Image Processing Toolbox, it's just two real lines of code, a call to regionprops and a line to extract the lengths from what regionprops returns.
% Create sample binary data.
binaryArray = [0 0 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1]
% Measure lengths of stretches of 1's.
measurements = regionprops(logical(binaryArray), 'Area');
% Convert from structure to simple array of lengths.
allLengths = [measurements.Area]
% If you want 0's in between for some reason:
out = zeros(1, 2*length(allLengths)+1);
out(2:2:end) = allLengths
5000 numbers is no problem. This code can handle millions of them.

1 commentaire

Joseph Cheng
Joseph Cheng le 22 Juil 2014
Modifié(e) : Joseph Cheng le 22 Juil 2014
if you don't have the Image Processing Toolbox (or those who find this post trying to do something similar) you can do something like this:
Zs = randi(10,1,10)+1; %number of zeros in a row.
Os = randi(10,1,10)+1; %number of ones in a row.
s = [];
for ind = 1:10
s = [s ones(1,Os(ind)) zeros(1,Zs(ind))]
end
s = strtrim(num2str(s')')
s = ['0' s '0'] %start the values with something you know.
b_bin = logical(s(:)'-'0') %
ds = diff(b_bin) %coincidentally diff of s would work as 1 and 0 strings are 1 value apart.
result = diff(find(abs(ds)==1)); %find transitions
result(2:2:end) = 0 %since forced the start of the array to zero we know the odd indexes are consecutive 1 and even indexes are consecutive 0's (ignoring the leading and trailing zeros).

Connectez-vous pour commenter.

Plus de réponses (3)

Wayne King
Wayne King le 22 Juil 2014

0 votes

Hi Sasha, I'm presuming your binary number is a character array:
s = '001111000000011100000000011111';
K1 = strfind(s,'1');
F = diff(find([1 diff(K1 - (1:length(K1)))]));
splitvec = mat2cell(K1,1,[F length(K1)-sum(F)]);
NumConsec1 = cellfun(@numel,splitvec);
NumConsec1 gives you the number of consecutive 1's. splitvec is a cell array with the actual indices of those ones, whicy you can see if you enter
splitvec{:}

1 commentaire

Shasha Glow
Shasha Glow le 22 Juil 2014
Modifié(e) : Shasha Glow le 22 Juil 2014
hi wayne.. actually m not working on short array.... it is very lengthy array which contain more than 5000 binary numbers... so i am not able to run this code because its only work on short arrays... do you have any other idea to count the 1's?

Connectez-vous pour commenter.

Laszlo
Laszlo le 14 Déc 2016
Modifié(e) : Laszlo le 14 Déc 2016

0 votes

What about this:
series_length=find(diff(binary_series)==-1)-find(diff(binary_series)==1);
you might have to pad binary_series with 0s at the start and end to ensure switch on and off.

1 commentaire

Image Analyst
Image Analyst le 14 Déc 2016
Does not work:
% Create sample binary data.
binary_series = [0 0 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1]
% Laslo's code below:
series_length=find(diff(binary_series)==-1)-find(diff(binary_series)==1)
Complete error message:
Matrix dimensions must agree.
Error in test3 (line 4)
series_length=find(diff(binary_series)==-1)-find(diff(binary_series)==1)

Connectez-vous pour commenter.

Anshuk Uppal
Anshuk Uppal le 16 Fév 2018
Modifié(e) : Walter Roberson le 16 Fév 2018

0 votes

A working tested algorithm -
n_ofErrors=flip(find(diff(error_vector)==-1))(1:1:length(find(diff(error_vector)==1))) - flip(find(diff(error_vector)==1));

6 commentaires

Anshuk Uppal
Anshuk Uppal le 16 Fév 2018
just pad the error vector with zeroes error_vector=[0 error_vector 0];
Guillaume
Guillaume le 16 Fév 2018
This is certainly not a working algorithm. The sequence of characters |)(| is never valid in matlab code. This will result in
Error: ()-indexing must appear last in an index expression.
Anshuk Uppal
Anshuk Uppal le 16 Fév 2018
The algorithm certainly does work, but not in matlab. It has been tested in an open source version-octave
Guillaume
Guillaume le 16 Fév 2018
Well, this is a matlab forum, not an octave forum...
Anshuk Uppal
Anshuk Uppal le 16 Fév 2018
You can make it work by truncating the array first and not using the whole expression in a single line. That should solve the error matlab generates. An algorithm is a series of instructions(may be mathematical) that solve a problem. Differences in syntax can occur...
Guillaume
Guillaume le 16 Fév 2018
Well, yes. And you can make your algorithm a lot more efficient by performing diff and find only once rather than 3 times each. I also don't understand the purpose of the flip.
transitions = find(diff([0; error_vector(:); 0]));
n_ofErrors = transitions(2:2:end) - transitions(1:2:end)

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by