How can I not count some rows within X,Y columns that contain X>=3 to alter the bin counts of Y for a histogram?

1 view (last 30 days)
I have a set of X,Y data that I am using to create a histogram. I am using Y to create the bins and my histograms turn exactly how I want them. But now I want to only include values of X >= 3 to make an additional histogram at the end of the code. How can I relate the X data to Y data to not count/eliminate those rows to bin a new condensed Y column? I'm not sure if there is a function I am missing that's not the toDelete function or an if else condition (which I haven't been able to get it to work). I don't want to delete my data completely, as with toDelete, since the histograms at the beginning of the code are affected.
Here is the code I am using to make my histograms:
X=data1.x
Y=data1.y
binranges=[0:2:30];
bincounts=histc(Y,binranges);
barh(binranges,bincounts)

Answers (1)

Steven Lord
Steven Lord on 14 Jun 2021
If I understand your goal correctly I think you want to call histogram with the 'BinLimits' name-value pair.
  3 Comments
Steven Lord
Steven Lord on 14 Jun 2021
Looking more closely, where does X even enter the picture? You define it on the first line of your code but then it does not appear on any of the subsequent lines.
If you want to filter those elements of Y whose corresponding elements in X are greater than 3:
x = 2 + randn(1, 1e5);
y = x.^2;
figure
histogram(y, 'BinEdges', 1:11);
figure
histogram(y(x < 3), 'BinEdges', 1:11);
There's nothing in any bin past [8, 9) since all those elements of y would correspond to elements in x that were greater than or equal to 3 and we excluded those with logical indexing.
Demi Girot
Demi Girot on 14 Jun 2021
This worked! Yes, X is no where in the code which I was trying to show since I had no idea how to make that connection when Y was the focus for the histogram. Thank you for your help.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!