How do I put my values in certain bins and retrieve the values from the highest count bin
6 views (last 30 days)
Show older comments
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 Comments
Bruno Luong
on 29 Jul 2022
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)
Accepted Answer
Bruno Luong
on 29 Jul 2022
Edited: Bruno Luong
on 29 Jul 2022
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 Comments
More Answers (0)
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!