現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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.
johnson saldanha
2018 年 11 月 7 日
after i am done assigning how do the display the values in each bin.
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
>>
johnson saldanha
2018 年 11 月 12 日
could u tell me how i can see what the values in a certain bin are
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 件のコメント
johnson saldanha
2018 年 11 月 12 日
after i am done assigning how do the display the values in each bin
参考
カテゴリ
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)