Averaging the 3D-array
1 view (last 30 days)
Show older comments
Vadim Tambovtsev
on 8 Oct 2016
Answered: Andrei Bobrov
on 8 Oct 2016
Hello. Suppose we have an 3D-array 4x4x4 with some values in it. The values are either 5 or 7.
A(:,:,1)=[5 5 7 7; 7 5 7 7; 5 5 5 5; 7 7 7 7];
A(:,:,2)=[5 5 7 7; 7 5 7 7; 5 5 5 5; 7 7 7 7];
A(:,:,3)=[5 5 7 7; 7 5 7 7; 5 5 5 5; 7 7 7 7];
A(:,:,4)=[5 5 7 7; 7 5 7 7; 5 5 5 5; 7 7 7 7];
How to refine this grid based on the majority rule? To make 2x2x2 grid, f.ex. Anew(1:1:1)= from 8 values of the blocks x1y1z1,x2y1z1,x1y2z1,x2y2z1,x1y1z2,x2y1z2,x1y2z2,x2y2z2 the values are 3 "5"s and 5 "7"th, so Anew(1:1:1)=7. If number of "5"s and "7"s is the same, choose "7".
The question is not easy, I think. Thank you!
0 Comments
Accepted Answer
Massimo Zanetti
on 8 Oct 2016
You may want to histogram the 3D array by 3D histogram. There many functions in Matlab central, just search for "N dimensional histogram".
7 Comments
More Answers (2)
Jan
on 8 Oct 2016
If you realy have 2 values only, convert them to 0 and 1:
A(:,:,1)=[5 5 7 7; 7 5 7 7; 5 5 5 5; 7 7 7 7];
A(:,:,2)=[5 5 7 7; 7 5 7 7; 5 5 5 5; 7 7 7 7];
A(:,:,3)=[5 5 7 7; 7 5 7 7; 5 5 5 5; 7 7 7 7];
A(:,:,4)=[5 5 7 7; 7 5 7 7; 5 5 5 5; 7 7 7 7];
B = double(A == 7);
B = reshape(B, [2, 2, 2, 2, 2, 2]);
C = squeeze(sum(sum(sum(B, 1), 3), 5)) >= 4;
Result = 5 + C * 2;
% Or:
Result = repmat(5, [2,2,2]);
Result(C) = 7;
0 Comments
Andrei Bobrov
on 8 Oct 2016
B = mat2cell(A,[2,2],[2,2],[2,2]);
out = zeros(size(B));
for ii = 1:numel(out)
[a,~,c] = unique(B{ii}(:));
jj = accumarray(c,1);
k = sortrows([a,jj],[-2,-1]);
out(ii) = k(1);
end
0 Comments
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!