Averaging the 3D-array

1 visualizzazione (ultimi 30 giorni)
Vadim Tambovtsev
Vadim Tambovtsev il 8 Ott 2016
Risposto: Andrei Bobrov il 8 Ott 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!

Risposta accettata

Massimo Zanetti
Massimo Zanetti il 8 Ott 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 Commenti
Vadim Tambovtsev
Vadim Tambovtsev il 8 Ott 2016
I haven't managed to get "random" values in myMode. Can you please tell me the answer?
Massimo Zanetti
Massimo Zanetti il 8 Ott 2016
m=C{1}( randi(numel(C{1})) );

Accedi per commentare.

Più risposte (2)

Jan
Jan il 8 Ott 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;

Andrei Bobrov
Andrei Bobrov il 8 Ott 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

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by