Summing identical submatricies to get a smaller final matrix
1 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
Here is my problem, I have a 2500x2500 matrix, this can be thought of as (50x50)x(50x50). I would like to sum each 50x50 sub-matrix and output a smaller 50x50 matrix where each point in the matrix represents the sum of each larger 50x50 matrix.
I have tried using blockproc:
if true
b_cx=JXrPrimex.*InvrMPC;
fun = @(block_struct) ...
sum(b_cx)
Bcx=blockproc(b_cx,[50 50], fun);
% code
end
However, when I run 'whos Bcx' I get a value that isn't a 50x50 matrix.
I also experimented with mat2cell but thought this seemed inefficient as after breaking each matrix down into 50x50 submatricies, I would then have to sum them all and then reconstruct a matrix of these sums. This is because my final answers needs to be in matrix not cell form.
Hope I was clear, if I wasn't comment and I will do my best to explain.
Thanks, John.
0 个评论
采纳的回答
Guillaume
2016-2-4
Firstly, sum applied to a matrix return a row vector, not a scalar. Secondly, your fun function does not work on a block but on the whole matrix. Your fun function should be:
fun = @(block_struct) sum(block_struct.data(:)); %or sum(sum(block_struct.data))
As you said mat2cell would also work, the code is very simple and in my testing a lot faster than blockproc:
b_cx=JXrPrimex.*InvrMPC;
split_bcx = mat2cell(b_cx, ones(1, size(b_cx, 1)/50)*50, ones(1, size(b_cx, 2)/50)*50);
Bcx = cellfun(@(c) sum(c(:)), split_bcx);
0 个评论
更多回答(1 个)
Matt J
2016-2-12
A=reshape(yourMatrix,50,50,50,50);
B=sepblockfun(A,[50,50,1,1],'sum');
result = reshape(B,[50,50]);
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!