Binning with nested for and if/else loops
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Image Analyst
2014-2-13
Can't you just histogram it with hist() or histc()???
10 个评论
Image Analyst
2014-2-14
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!