이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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 (한국어)