Averaging the 3D-array

1 回表示 (過去 30 日間)
Vadim Tambovtsev
Vadim Tambovtsev 2016 年 10 月 8 日
回答済み: Andrei Bobrov 2016 年 10 月 8 日
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!

採用された回答

Massimo Zanetti
Massimo Zanetti 2016 年 10 月 8 日
You may want to histogram the 3D array by 3D histogram. There many functions in Matlab central, just search for "N dimensional histogram".
  7 件のコメント
Vadim Tambovtsev
Vadim Tambovtsev 2016 年 10 月 8 日
I haven't managed to get "random" values in myMode. Can you please tell me the answer?
Massimo Zanetti
Massimo Zanetti 2016 年 10 月 8 日
m=C{1}( randi(numel(C{1})) );

サインインしてコメントする。

その他の回答 (2 件)

Jan
Jan 2016 年 10 月 8 日
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 2016 年 10 月 8 日
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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by