Summing identical submatricies to get a smaller final matrix

2 vues (au cours des 30 derniers jours)
John Draper
John Draper le 4 Fév 2016
Réponse apportée : Matt J le 12 Fév 2016
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.

Réponse acceptée

Guillaume
Guillaume le 4 Fév 2016
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);

Plus de réponses (1)

Matt J
Matt J le 12 Fév 2016
A=reshape(yourMatrix,50,50,50,50);
B=sepblockfun(A,[50,50,1,1],'sum');
result = reshape(B,[50,50]);

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by