How do I put my values in certain bins and retrieve the values from the highest count bin
13 次查看(过去 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 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!