フィルターのクリア

sum every 2 pages at a time in 3D matrix

2 ビュー (過去 30 日間)
AJ
AJ 2018 年 7 月 13 日
編集済み: James Tursa 2018 年 7 月 13 日
I'm trying to sum every 2 pages at a time in 3D data.
for example, suppose I have 3x2x5 double data like this:
data = ones(3,2,5);
after I sum every 2 pages then I can get 3x2x3 double data like this:
data(:,:,1) =
2 2
2 2
2 2
data(:,:,2) =
2 2
2 2
2 2
data(:,:,3) =
1 1
1 1
1 1
I'm not sure you can understand what I'm saying..please leave comment if you need more explanation.
The JPG file I attached(pic.jpg) might help you understand.
Many thanks to all

採用された回答

James Tursa
James Tursa 2018 年 7 月 13 日
編集済み: James Tursa 2018 年 7 月 13 日
Another way:
p = 2; % number of pages at a time to sum
datas = data;
m = mod(size(datas,3),p);
if( m )
datas(end,end,end+p-m) = 0; % pad if needed
end
datas = reshape(datas,size(datas,1),size(datas,2),p,size(datas,3)/p);
result = sum(datas,3);
result = reshape(result,size(data,1),size(data,2),[]);

その他の回答 (1 件)

Rik
Rik 2018 年 7 月 13 日
The code below isn't the most efficient for your example, but will work in more complicated situations.
data = ones(3,2,5);
k=2;%number of last dimension to be summed
d=numel(size(data));sumdata_part=cell(k,1);
for k2=1:k
% This part doesn't support other dimensional examples,
% but that can be done as well if you need it to.
sumdata_part{k2}=data(:,:,k2:k:end);
end
%ensure same sizes
temp=zeros(size(sumdata_part{1}));
temp(1:numel(sumdata_part{end}))=sumdata_part{end};
sumdata_part{end}=temp;
%concatenate in a new direction and sum over that dimension
outcome=sum(cat(d+1,sumdata_part{:}),d+1)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by