You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how do i discretize negative integers
1 view (last 30 days)
Show older comments
[~, 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
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
johnson saldanha
on 6 Nov 2018
i got an error which said "Edge vector must be monotonically non-decreasing"
Stephen23
on 6 Nov 2018
Edited: Stephen23
on 6 Nov 2018
@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.
Bruno Luong
on 6 Nov 2018
HISTC tolerates duplicated edges, INTERP1 however doesn't.
johnson saldanha
on 6 Nov 2018
got it now. thanks
Bruno Luong
on 6 Nov 2018
@johnson: see my post above to see how you organize the edge: it must decreases in absolute value if they are negative
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.
johnson saldanha
on 6 Nov 2018
thanks for replies. helped a lot
johnson saldanha
on 7 Nov 2018
after i am done assigning how do the display the values in each bin.
johnson saldanha
on 12 Nov 2018
whats idx?
johnson saldanha
on 12 Nov 2018
yes i did try the same but i got an error which says matrix dimensions must agree
johnson saldanha
on 12 Nov 2018
Edited: Stephen23
on 12 Nov 2018
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
Walter Roberson
on 12 Nov 2018
Transpose the output of unique. The code needs a row vector at that point.
Stephen23
on 12 Nov 2018
Edited: Stephen23
on 12 Nov 2018
@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
johnson saldanha
on 12 Nov 2018
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
Stephen23
on 12 Nov 2018
@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.
johnson saldanha
on 12 Nov 2018
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
Walter Roberson
on 12 Nov 2018
You have some input values that are outside of any of the bins. What do you want to do with those?
johnson saldanha
on 12 Nov 2018
best option is to discard i feel
johnson saldanha
on 12 Nov 2018
the output of bin is just 20. its not displaying what the values in a certain bin are
Bruno Luong
on 12 Nov 2018
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
>>
johnson saldanha
on 12 Nov 2018
could u tell me how i can see what the values in a certain bin are
johnson saldanha
on 12 Nov 2018
but the output for bin returns only the number of bins i.e 20. it doesnt give me any values
Bruno Luong
on 12 Nov 2018
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".
Stephen23
on 12 Nov 2018
Edited: Stephen23
on 12 Nov 2018
"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.
johnson saldanha
on 12 Nov 2018
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
on 12 Nov 2018
Edited: Bruno Luong
on 12 Nov 2018
DO YOUR WORK, GIVE AN EXAMPLE PLEASE AS REQUESTED!!!!
johnson saldanha
on 12 Nov 2018
@Stephen Cobeldick. im not able to see those values
johnson saldanha
on 12 Nov 2018
Edited: madhan ravi
on 12 Nov 2018
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.
Bruno Luong
on 12 Nov 2018
Edited: Bruno Luong
on 12 Nov 2018
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{:}
Stephen23
on 12 Nov 2018
Edited: Stephen23
on 12 Nov 2018
@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)
johnson saldanha
on 12 Nov 2018
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
johnson saldanha
on 12 Nov 2018
@StephenCobeldick. im getting the answer as 20. thats it
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.
More Answers (1)
Bruno Luong
on 6 Nov 2018
"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
johnson saldanha
on 12 Nov 2018
after i am done assigning how do the display the values in each bin
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)