Effacer les filtres
Effacer les filtres

Binning with nested for and if/else loops

2 vues (au cours des 30 derniers jours)
B.M.
B.M. le 13 Fév 2014
Commenté : Image Analyst le 14 Fév 2014
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
Can't you just histogram it with hist() or histc()???
  10 commentaires
B.M.
B.M. le 13 Fév 2014
Thanks Matt, I think you are right. Thanks to both of you!
Image Analyst
Image Analyst le 14 Fév 2014
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 Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by