How to split data set into multiple bins and perform condition statement on bins

I have a logical data set and I am trying to divide it into 31 bins but my data points are not evenly distributing and I need it to be divided into 31 bins so that I can then run an if statement that counts the total number of ones in each bin and compares it to a condition.

Réponses (1)

Matt J
Matt J le 24 Mar 2022
Modifié(e) : Matt J le 24 Mar 2022
Use the discretize() command.
to assign bin labels to each of your data points.

11 commentaires

Also, splitapply() is useful for applying a function on data with a common bin label.
Which discretize() command would I use? I just want to assign my data points into 31 bins. Will it do it even if my data can’t be distributed evenly between the bins?
Which discretize() command would I use?
The one at the link I gave you.
Will it do it even if my data can’t be distributed evenly between the bins?
You will see on the documentation page, that you can specify the bin boundaries.
How do I get the edges to work if it’s only ones and zeros?
Matt J
Matt J le 25 Mar 2022
Modifié(e) : Matt J le 25 Mar 2022
I can't envision the difficulty you're hinting at. You need to provide an example of a (preferably small) set of data you want to bin, and the desired results.
Let’s say I have a data set Data = [0 0 0 1 0 1 1 0 1 1 1 0] And I wanted to split that data set into 3 smaller data sets. Then I wanted to count the amount of ones in each subset and if it is greater or equal to 1 then count that event as a one. Then count the total amount of occurrences.
Let’s say I have a data set Data = [0 0 0 1 0 1 1 0 1 1 1 0] And I wanted to split that data set into 3 smaller data sets.
According to what rule are the elements to be assigned to different bins in this example? I thought that was the essence of your question. If I were to assign them to bins randomly, I could proceed as follows:
Data = [0 0 0 1 0 1 1 0 1 1 1 0];
binLabels=randi(3,1,numel(Data))
binLabels = 1×12
2 3 1 3 3 1 1 3 1 1 1 3
numOnes=accumarray(binLabels',Data')
numOnes = 3×1
5 0 1
As a check on this, we can form the 3 groups by doing,
groups = splitapply(@(x){x},Data,binLabels )
groups = 1×3 cell array
{[0 1 1 1 1 1]} {[0]} {[0 1 0 0 0]}
You can see that numOnes gives the correct tally per group are accurate.
and if it is greater or equal to 1 then count that event as a one. Then count the total amount of occurrences.
Simply do,
overallCount = sum(numOnes>=1)
overallCount = 2
Any way to not randomly assign them to bins? I need the data to be distributed into 31 bins as it is.
Matt J
Matt J le 25 Mar 2022
Modifié(e) : Matt J le 25 Mar 2022
Yes, you should tell us how you would like them distributed. I have already asked you to accompany your example with the desired output.
With my example I want the data to be distributed into 3 subsets of data. I then want it to count the total amount of ones in the set and if it is great than 1 count that event as a one and then take the total count of events. So my example should give me a count of 3.
So my example should give me a count of 3.
No, that would depnd on how the data is split into subsets. Even without randomization, a subset may contain no ones, like in the following, where the subsets are sequential.
Data = [0 0 0 1 0 1 1 0 1 1 1 0];
binLabels=[1 1 1 2 2 2 2 3 3 3 3 3];
numOnes=accumarray(binLabels',Data')
numOnes = 3×1
0 3 3
overallCount = sum(numOnes>=1)
overallCount = 2
Once again, because you haven't specified the details of the processing with enough care and detail, we get a result you don't expect. Perhaps you meant for the subsets to be interleaved. That does give your expected result:
binLabels=[1 2 3 1 2 3 1 2 3 1 2 3];
numOnes=accumarray(binLabels',Data')
numOnes = 3×1
3 1 2
overallCount = sum(numOnes>=1)
overallCount = 3

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB Parallel Server dans Centre d'aide et File Exchange

Modifié(e) :

le 25 Mar 2022

Community Treasure Hunt

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

Start Hunting!

Translated by