How do I put my values in certain bins and retrieve the values from the highest count bin
10 ビュー (過去 30 日間)
古いコメントを表示
I have been trying to do this for a while and failed, am hopping to get some guidance. So I have a 3D variable called Zo, its 3601 by 7201 by 100, and all the values in it range from 0 to 1. What I would like to do is sort my values while ignoring values of Zo that are exactly zero. For example, if I am investigating Zo(1,1,:), I would like to sort the 100 values in Zo(1,1,:) that are not zeros into 10 bins that each has 0.1 width. And after, I would like to retain the values of Zo(1,1,:) that fall under the bin with the highest count. The final result should be a new variable called N that has the non-zero values of the bin with the highest count, and another variable called M that has the correspodning index of the values picked out from Zo(1,1,:). I hope I explained it well and I appreciate any guidance.
3 件のコメント
Bruno Luong
2022 年 7 月 29 日
But again in your description
"I would like to sort the 100 values in Zo(1,1,:) that are not zeros into 10 bins"
Still your decription still mix both, sort is rearange the data, binning means find where is the bin the data falls into. Please be precise.
a = rand(1,20)
sort(a)
binloc = discretize(a, 0:0.1:1)
採用された回答
Bruno Luong
2022 年 7 月 29 日
編集済み: Bruno Luong
2022 年 7 月 29 日
Adapt this to your need
% Example data
A=rand(2,3,20);
A(randi(numel(A),1,10))=0;
[m,n,p] = size(A);
edges = 0:0.1:1;
A(A==0)=NaN;
B = discretize(A,edges);
[I,J] = ndgrid(1:m,1:n);
I = repmat(I,1,1,p);
J = repmat(J,1,1,p);
IJK = [I(:) J(:) B(:)];
IJK = IJK(B >= 1,:);
q = length(edges)-1;
bincount = accumarray(IJK, 1, [m,n,q]);
[highestcount, binnum] = max(bincount,[],3)
ilin = find(B==binnum);
[i,j,k] = ind2sub(size(B), ilin);
slide = accumarray([i(:) j(:)], k(:), [m,n], @(k) {k.'});
Avalue = accumarray([i(:) j(:)], A(ilin(:)), [m,n], @(v) {v.'});
[r,c] = ndgrid(1:m,1:n);
r = r(:); c = c(:); slide = slide(:); Avalue=Avalue(:); binnum = binnum(:);
T = table(r, c, binnum, slide, Avalue)
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Numeric Types についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!