Borrar filtros
Borrar filtros

Binning with nested for and if/else loops

2 visualizaciones (últimos 30 días)
B.M.
B.M. el 13 de Feb. de 2014
Comentada: Image Analyst el 14 de Feb. de 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

Respuesta aceptada

Image Analyst
Image Analyst el 13 de Feb. de 2014
Can't you just histogram it with hist() or histc()???
  10 comentarios
B.M.
B.M. el 13 de Feb. de 2014
Thanks Matt, I think you are right. Thanks to both of you!
Image Analyst
Image Analyst el 14 de Feb. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by