how do i discretize negative integers
1 次查看(过去 30 天)
显示 更早的评论
[~, 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?
采纳的回答
Stephen23
2018-11-6
编辑:Stephen23
2018-11-6
"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 个评论
johnson saldanha
2018-11-6
i got an error which said "Edge vector must be monotonically non-decreasing"
Stephen23
2018-11-6
编辑:Stephen23
2018-11-6
@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
2018-11-6
@johnson: see my post above to see how you organize the edge: it must decreases in absolute value if they are negative
Stephen23
2018-11-6
编辑:Stephen23
2018-11-6
"...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.
Stephen23
2018-11-7
>> 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
johnson saldanha
2018-11-12
yes i did try the same but i got an error which says matrix dimensions must agree
Stephen23
2018-11-12
@johnson saldanha: please show the complete error message. This means all of the red text.
johnson saldanha
2018-11-12
编辑:Stephen23
2018-11-12
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
2018-11-12
Transpose the output of unique. The code needs a row vector at that point.
Stephen23
2018-11-12
编辑:Stephen23
2018-11-12
@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
2018-11-12
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
2018-11-12
@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
2018-11-12
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
2018-11-12
You have some input values that are outside of any of the bins. What do you want to do with those?
johnson saldanha
2018-11-12
the output of bin is just 20. its not displaying what the values in a certain bin are
Bruno Luong
2018-11-12
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
>>
Stephen23
2018-11-12
"could u tell me how i can see what the values in a certain bin are"
Four days ago:
johnson saldanha
2018-11-12
but the output for bin returns only the number of bins i.e 20. it doesnt give me any values
Bruno Luong
2018-11-12
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
2018-11-12
编辑:Stephen23
2018-11-12
"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
2018-11-12
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
2018-11-12
编辑:Bruno Luong
2018-11-12
DO YOUR WORK, GIVE AN EXAMPLE PLEASE AS REQUESTED!!!!
johnson saldanha
2018-11-12
编辑:madhan ravi
2018-11-12
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
2018-11-12
编辑:Bruno Luong
2018-11-12
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
2018-11-12
编辑:Stephen23
2018-11-12
@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
2018-11-12
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
Bruno Luong
2018-11-12
编辑:Bruno Luong
2018-11-12
So? Common: YOU make a change (column #2) that breaks the code, so don't complain to me.
更多回答(1 个)
Bruno Luong
2018-11-6
"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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)