how do i discretize negative integers

[~, discrete_x] = histc(x, edges);
discrete_x(discrete_x == length(edges)) = length(edges)-1;
discrete_x(discrete_x == 0) = NaN;
This works for positive integers only. what do i do if i have to do it for negative integers?

 Accepted Answer

Stephen23
Stephen23 on 6 Nov 2018
Edited: Stephen23 on 6 Nov 2018
"This works for positive integers only"
Actually histc works perfectly for negative values. It works for me:
>> x = 4-randi(9,1,10)
x =
-2 -5 -5 1 -1 -5 1 1 2 0
>> edges = -6:4:6
edges =
-6 -2 2 6
>> [~, idx] = histc(x, edges)
idx =
2 1 1 2 2 1 2 2 3 2
>> vec = x(idx)
vec =
-5 -2 -2 -5 -5 -2 -5 -5 -5 -5

38 Comments

i got an error which said "Edge vector must be monotonically non-decreasing"
@johnson saldanha: then your edge vector is not in order or contains duplicate values.
Solution: if there are no duplicates then you could sort it before using it:
histc(...,sort(edges))
If it contains duplicates then those need to be removed, one easy solution is to use unique:
histc(...,unique(edges))
However I highly recommend that you fix the code that creates the edges vector, to ensure that it is monotonic increasing, right from the start.
HISTC tolerates duplicated edges, INTERP1 however doesn't.
got it now. thanks
@johnson: see my post above to see how you organize the edge: it must decreases in absolute value if they are negative
Stephen23
Stephen23 on 6 Nov 2018
Edited: Stephen23 on 6 Nov 2018
"...it must decreases in absolute value if they are negative"
if they are all negative... but it gests more complex than that if there are both negative and positive edge values, it goes back to monotonic increasing. Either this a bug, or badly designed.
Possibly histogram et al avoid all of this.
thanks for replies. helped a lot
after i am done assigning how do the display the values in each bin.
>> for bin = unique(idx), bin, x(bin==idx), end
bin = 1
ans = -5 -5 -5
bin = 2
ans = -2 1 -1 1 1 0
bin = 3
ans = 2
"whats idx?"
You can see idx defined in my answer:
[~, idx] = histc(x, edges)
Read the histc help to know what it represents.
yes i did try the same but i got an error which says matrix dimensions must agree
@johnson saldanha: please show the complete error message. This means all of the red text.
Error using ==
Matrix dimensions must agree.
Error in rider_pattern_mod (line 154)
for bin = unique(discrete_xm_dec(:,3)), bin, xm(bin==discrete_xm_dec(:,3)), end
Transpose the output of unique. The code needs a row vector at that point.
@johnson saldanha: do not put multiple separate pieces of code on line like that. Doing so makes code hard to understand and debug, and this is a factor in the bug you have.
The input to unique is a column vector which means that its output will be a column vector. The for iterator is defined to be the columns of its input array (not many beginners bother to read the documentation to find that out, but they should), so your loop will iterate exactly once with bin being a column vector. Then there is absolutely nothing in your code that ensure that the column vector bin has the same number of elements as the column vector discrete_xm_dec, thus you will get that error.
Basically the error is a side-effect putting everything onto one line, which made your code harder to follow. You can fix this simply by writing neater code and not providing a column vector as the for input:
U = unique(discrete_xm_dec(:,3));
for bin = U(:).' % row vector
bin
xm(bin==discrete_xm_dec(:,3))
end
thanks. will follow this. there are NaNs in between. as a result im getting NaN as final answer. in which line can i include omitnan
@johnson saldanha: you will have to remove NaN's before histc or unique, i.e. basically before your entire code. NaN's cannot be binned, and will each be considered unique, so there is not much point in including them.
there is no NaN in my values. but after binning, the matrix which displays the bin assigned has NaNs. NaN is created after using the command histc
You have some input values that are outside of any of the bins. What do you want to do with those?
best option is to discard i feel
the output of bin is just 20. its not displaying what the values in a certain bin are
johnson saldanha "NaN is created after using the command histc"
Never, impossible. If HISTC can run, it returns "0" for data outside the edge ranges, including NaN input, and meaningful bin count and loc otherwise. It never returns NaN.
>> [c,n] = histc([nan nan 1.5 1.6 ],[1 2 3 4])
c =
2 0 0 0
n =
0 0 1 1
>>
could u tell me how i can see what the values in a certain bin are
but the output for bin returns only the number of bins i.e 20. it doesnt give me any values
Your description is vague. Because when display "n" (the second output of HISTC) I can see which bin the values "x" are falling into.
Now what you want to "see" more, I have no idea. That's why we keep asking for users to post the short example what they expect to "see".
"but the output for bin returns only the number of bins i.e 20. it doesnt give me any values"
What does that have to do with the comment that I linked to?
This is the code I gave you four days ago:
>> for bin = unique(idx), bin, x(bin==idx), end
bin = 1
ans = -5 -5 -5 % !!! VALUES !!!
bin = 2
ans = -2 1 -1 1 1 0 % !!! VALUES !!!
bin = 3
ans = 2 % !!! VALUES !!!
The marked lines show the values from the random data matrix that my example used. You can also see the bin numbers.
once i have put the values in the bin and i can see which bin the values fall into. i want to the values from different bins. for ex. if i access 1st bin, i want to know what values it contains.
Bruno Luong
Bruno Luong on 12 Nov 2018
Edited: Bruno Luong on 12 Nov 2018
DO YOUR WORK, GIVE AN EXAMPLE PLEASE AS REQUESTED!!!!
@Stephen Cobeldick. im not able to see those values
U = unique(discrete_xm_vel(:,2));
for bin = U(:).' % row vector
bin;
xm(bin==discrete_xm_vel(:,2));
end
The output for this is only bin=20. whereas in the example u gave you are getting the values. im not getting those.
The matreix tells me what bin the values fall into. but now i want to access a bin for ex Bin1 and i want all the values that are in that bin in a matrix.
n=20;
x=randn(1,n)
edges = (-4:0);
xx = x(:);
[c,loc] = histc(xx, edges);
in = loc>0;
m = length(edges);
bincontents = accumarray(loc(in),xx(in),[m,1],@(x){sort(x(:))'});
bincontents{:}
@johnson saldanha: if you add semicolons onto the end of each line then you suppress printing of the output. Get rid of the semicolons inside the loop, e.g. for bin:
bin
not
bin;
^ why did you add these? semicolon -> does not print.
Or alternatively you can put that code inside a disp call, e.g.:
disp(bin)
Attempted to access xm(:,2); index out of bounds because size(xm)=[19,1].
Error in @(xm){sort(xm(:,2))'}
Error in rider_pattern_mod (line 177) bincontents = accumarray(discrete_xm_vel(in),xm(in),[m,1],@(xm){sort(xm(:,2))'}); im getting this error
@StephenCobeldick. im getting the answer as 20. thats it
Bruno Luong
Bruno Luong on 12 Nov 2018
Edited: Bruno Luong on 12 Nov 2018
So? Common: YOU make a change (column #2) that breaks the code, so don't complain to me.

Sign in to comment.

More Answers (1)

"This works for positive integers only."
Wrong claim. It works for negative numbers,
histc(-1.5,[-3 -2 -1])
ans =
0 1 0
It only edges to be increased, meaning decrease in the absolute values

1 Comment

after i am done assigning how do the display the values in each bin

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!