# How do I put my values in certain bins and retrieve the values from the highest count bin

6 views (last 30 days)
Ali Almakhmari on 29 Jul 2022
Edited: Bruno Luong on 29 Jul 2022
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.
Bruno Luong on 29 Jul 2022
"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)
a = 1×20
0.2156 0.7017 0.9893 0.2969 0.5716 0.4926 0.1556 0.7251 0.1279 0.5596 0.2909 0.0361 0.7691 0.8392 0.7579 0.2132 0.4142 0.5608 0.5313 0.0022
sort(a)
ans = 1×20
0.0022 0.0361 0.1279 0.1556 0.2132 0.2156 0.2909 0.2969 0.4142 0.4926 0.5313 0.5596 0.5608 0.5716 0.7017 0.7251 0.7579 0.7691 0.8392 0.9893
binloc = discretize(a, 0:0.1:1)
binloc = 1×20
3 8 10 3 6 5 2 8 2 6 3 1 8 9 8 3 5 6 6 1

Bruno Luong on 29 Jul 2022
Edited: Bruno Luong on 29 Jul 2022
% 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)
highestcount = 2×3
3 4 4 4 5 4
binnum = 2×3
7 6 9 3 7 6
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)
T = 6×5 table
r c binnum slide Avalue _ _ ______ _______________ ______________________________________ 1 1 7 {[ 3 4 8]} {[ 0.6469 0.6739 0.6261]} 2 1 3 {[ 9 10 13 15]} {[ 0.2052 0.2092 0.2407 0.2721]} 1 2 6 {[ 3 7 8 15]} {[ 0.5394 0.5322 0.5644 0.5406]} 2 2 7 {[1 3 7 10 12]} {[0.6275 0.6546 0.6259 0.6836 0.6720]} 1 3 9 {[15 16 17 20]} {[ 0.8654 0.8613 0.8931 0.8730]} 2 3 6 {[ 9 14 16 19]} {[ 0.5944 0.5440 0.5698 0.5119]}

### Categories

Find more on Descriptive Statistics in Help Center and File Exchange

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!