Summing within an array to change the size

I have an array that has dimensions of 111x46x2. I want to sum the values in the first 3x3 block to become the first value of the next matrix repeating this until the last column is summed together. The dimensions of the new matrix should be 37x16x2.
I could make the dimensions of the original matrix 111x48x2 if that makes the calculations easier and more efficient. The 9x9 blocks will have values only in certain locations such as [ 0 0 #; # 0 0; 0 # 0]; and the last column will have values in each row.

4 件のコメント

Sean de Wolski
Sean de Wolski 2012 年 8 月 27 日
46 or 48?
Matthew Vincent
Matthew Vincent 2012 年 8 月 27 日
It would be easiest if I could keep the original array at 46 and the last "block" would be a 3x1 column but it would be possible to change it to 48 just inconvenient in the code.
Sean de Wolski
Sean de Wolski 2012 年 8 月 27 日
blockproc below is smart enough to handle either of those scenarios :)
Walter Roberson
Walter Roberson 2012 年 8 月 29 日
Please read the guide to tags and retag this Question; see http://www.mathworks.co.uk/matlabcentral/answers/43073-a-guide-to-tags

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

 採用された回答

Sean de Wolski
Sean de Wolski 2012 年 8 月 27 日
編集済み: Sean de Wolski 2012 年 8 月 27 日

0 投票

If you have the Image Processing Toolbox:
x = rand(111,46,2);
y = blockproc(x,[3 3],@(s)sum(sum(s.data,1),2));
Or if you don't:
z = convn(x,ones(3),'valid');
z = z(1:3:end,1:3:end,:); %may need to crop differently depending on last edges

1 件のコメント

Matthew Vincent
Matthew Vincent 2012 年 8 月 27 日
I think that the convn function will work for me. Have other things I need to incorporate before I can test it and be sure. Thanks

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

その他の回答 (2 件)

Matt Fig
Matt Fig 2012 年 8 月 27 日

0 投票

Here is one way to do it:
A = magic(6);
cellfun(@(x) sum(x(:)),mat2cell(A,2*ones(1,3),2*ones(1,3)))

4 件のコメント

Matthew Vincent
Matthew Vincent 2012 年 8 月 27 日
Is there anyway to expand this to be useful for an array input? I attempted to use num2cell but am not very familiar with that function
Sean de Wolski
Sean de Wolski 2012 年 8 月 27 日
編集済み: Sean de Wolski 2012 年 8 月 27 日
mat2cell is painfully slow for anything at all large. I recommend avoiding it:
x = rand(999,999,3);
tic,mat2cell(x,ones(1,333)*3,ones(1,333)*3,3);toc
Elapsed time is 7.549038 seconds.
Matt Fig
Matt Fig 2012 年 8 月 27 日
What do you mean 'array input'? A is an array....
Matt Fig
Matt Fig 2012 年 8 月 27 日
MAT2CELL is slow for large inputs, indeed. What are you MathWorkers doing with your time?! ;-)

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

Jan
Jan 2012 年 8 月 28 日
編集済み: Jan 2012 年 8 月 28 日

0 投票

You can use the fast FEX: BlockMean. Either modify the code or multiply the results accordingly.
A Matlab version:
x = rand(111, 46, 2);
S = size(X);
M = S(1) - mod(S(1), 3);
N = S(2) - mod(S(2), 3);
% Cut and reshape input such that the 1st and 3rd dimension have the lengths V
% and W:
XM = reshape(X(1:M, 1:N, :), 3, M / 3, 3, N / 3, []);
Y = squeeze(sum(sum(XM, 1), 3));

2 件のコメント

Andrei Bobrov
Andrei Bobrov 2012 年 8 月 29 日
with two reshape
s = size(x);
t = mod(-s(1:2),[3 3]);
xx = [x zeros(s(1),t(2),s(3))];
xx = [xx; zeros(t(1),s(2)+t(2),s(3))];
s2 = s + [t, 0];
y = reshape(sum(sum(reshape(xx,3, s2(1)/3, 3,[]),3)),s2(1)/3,s2(2)/3,[]);
Jan
Jan 2012 年 8 月 29 日
Difference between Andrei's and my solution:
  • RESHAPE instead of SQUEEZE (which calls RESHAPE internally)
  • Andrei appends zeros, I remove the not matching lines on the bottom.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by