# Not sure what this "histogram code" does in MATLAB

I have the following code that was given to me, but I'm not sure what the logic is. I suppose the idea is that this will result in a histogram / quantization of my data. Here is the code:

Entrance:

```
x = 180.*rand(1,1000); %1000 points from 0 to 180 degrees.
binWidth = 20; %I want the binWidth to be 20 degrees.
```

Main function:

`% ------------------------------------------------------------------------- % Compute the closest bin center x1 that is less than or equal to x % ------------------------------------------------------------------------- function [x1, b1] = computeLowerHistBin(x, binWidth) % Bin index bin = floor(x./binWidth - 0.5); % Bin center x1 x1 = binWidth * (bin + 0.5); % add 2 to get to 1-based indexing b1 = bin + 2; end`

Finally, the final "quantized" data:

```
w = 1 - (x - x1)./binWidth
```

Here's what I won't get: I don't understand - actually, why exactly is `x1`

calculated as it is, and also why / how is `w`

calculated as it is. In fact, of all things, `w`

it confuses me the most. I literally can't figure out the logic here, or what's really intended. Appreciate the detailed explanation of this logic. Thank.

source to share

It splits into `lb <= x < up`

and splits the interval `[0,180]`

into `[-10,10), [10, 30), [30,40) ..., [150,170), [170,190)`

.

Suppose that `x = 180`

, then:

```
bin = floor(180/20-0.5) = floor(9-0.5) = floor(8.5) = 8;
```

and if `x = 0`

:

```
bin = floor(`0/20-0.5) = floor(-0.5) = floor(-1) = -1;
```

which translate accordingly to `x1 = 20 * (8+0.5) = 170`

and `x1 = -10`

, which seems to be what the function suggests `lowerHistBin()`

.

At the end of the day, it `w`

simply measures how far the point is `x`

from the corresponding bottom bin `x1`

. Note that it `w`

is in (0,1), being `w`

equal to 1 for `x = x1`

and approaching 0 for `x -> x1+binWidth`

. Thus, if x says is approaching 170, it `w`

will approach `1 - (170-150)/20 = 0`

.

source to share