Binning with nested for and if/else loops

Hello all, I am trying to take a very long vector of distances and bin it. I'm indexing both the vector and a vector if bins. Currently the code returns all zeros, even though the distance bins should range from 0-165 (cm).
The basic idea is as follows: If a distance value is >=165 (for example), that value is then replaced with '165' (its bin). Same for every value down to 0.
If I did nested if/else, I understand that if/else stops once it meets a true condition, but I don't want to write if/else 34 times (the number of bins I have). Hence the indexing.
Here is my code. Can anyone tell me what I'm doing wrong?
=============================
bin=[164 158 142 122 108 97 67 44 28 10 6];
DistBin=[165:-5:0];
for BinIndex=1:length(bin);
for DBIndex=1:length(DistBin);
if bin(BinIndex) >= DistBin(DBIndex);
bin(BinIndex)=DistBin(DBIndex);
else
end
end
end
==================================
If I try to add an "else" statement to keep it stepping forward (as below) I get the same result.
==================================
bin=[164 158 142 122 108 97 67 44 28 10 6];
DistBin=[165:-5:0];
for BinIndex=1:length(bin);
for DBIndex=1:length(DistBin);
if bin(BinIndex) >= DistBin(DBIndex);
bin(BinIndex)=DistBin(DBIndex);
else bin(BinIndex) >= DistBin(DBIndex+1);
bin(BinIndex)=DistBin(DBIndex+1);
end
end
end

 Réponse acceptée

Image Analyst
Image Analyst le 13 Fév 2014

0 votes

Can't you just histogram it with hist() or histc()???

10 commentaires

B.M.
B.M. le 13 Fév 2014
Maybe? But I'm not trying to plot it, just replace the values with the bins they fall into.
Image Analyst
Image Analyst le 13 Fév 2014
Who says you need to plot it? You can just bin them into an array. No plotting is necessary.
B.M.
B.M. le 13 Fév 2014
If I do the following (adding 2.5 to my bins because they are centered by default) what I get is a weird almost logical-looking vector except there's one "2" value.
++++++++++++++++++++++++++++
bin=[164 158 142 122 108 97 67 44 28 10 6];
DistBin=[0:5:165]+2.5;
bin=hist(bin,DistBin)
data = [164 158 142 122 108 97 67 44 28 10 6];
binEdges = 0 : 5 : 165;
counts = histc(data, binEdges)
B.M.
B.M. le 13 Fév 2014
How does this make sense? It returns a logical vector.
Matt Kindig
Matt Kindig le 13 Fév 2014
Modifié(e) : Matt Kindig le 13 Fév 2014
No it doesn't. Look at 'counts' again--it is a double vector.
EDIT: I think this is what you want.
data = [164 158 142 122 108 97 67 44 28 10 6];
binEdges = 0 : 5 : 165;
[counts, bins] = histc( data, binEdges);
binnedData = binEdges(bins);
Image Analyst
Image Analyst le 13 Fév 2014
Well your bins are only 5 wide, and any given bin has only one data value in it, or none. So the values are 0 or 1. It's not a logical data type.
B.M.
B.M. le 13 Fév 2014
So this tells me which of my bins has a value that falls into it....and if I fed my true data vector in, I'd have big counts (say,40ish) for each bin.
But how do I get from that to replacing the original data with the bin they fall into? I just don't see it, sorry.
B.M.
B.M. le 13 Fév 2014
Thanks Matt, I think you are right. Thanks to both of you!
Oh, I see what you mean. So you want a value to be replaced by what bin number it would have been stuffed into. For that you want intlut() which is probably easiest, though you could also use imquantize(). Both require the Image Processing Toolbox. Do you have that? If not, try ceil:
bin=[164 158 142 122 108 97 67 44 28 10 6]
bin = ceil(bin/5) % Replace bin by what bin number the number would be in.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by