MATLAB: Binning with nested for and if/else loops

binningbinsfor loopsif elseImage Processing Toolboxindexing

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

Best Answer

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