adding matrices inside a big matrix

Hello everybody,
I am wandering if there is a way for not using loops to add many small matrices inside a big matrix. For example I have a big 40000x240000 matrix and I want to add all the numbers of 4x4 matrices inside that matrix, resulting in a 10000x60000 matrix.
thank you all for your answers

 채택된 답변

Sean de Wolski
Sean de Wolski 2011년 11월 15일

2 개 추천

And this fun answer:
sum4x4 = reshape(sum(reshape(reshape(sum(reshape(B,4,[])),size(B,1)/4,[])',4,[])),size(B)./4);
sum4x4 = reshape(sum4x4,size(sum4x4,2),size(sum4x4,1))'
where B is your matrix.

댓글 수: 6

Fangjun Jiang
Fangjun Jiang 2011년 11월 15일
+1, that is fun! Hope your brain is not re-shaped!
j_solar
j_solar 2011년 11월 15일
yeah, upps, hadn't changed magic(16). Did it now and it works, however it is slower than the mat2cell option. Not much but a bit slower.
The fun answer is much faster, over 100 times, however something doesn't work right. It seems like the values are the same, however they are not in the right position.
Sean de Wolski
Sean de Wolski 2011년 11월 15일
I forgot to transpose the whole matrix at the end, sorry. try it now!
j_solar
j_solar 2011년 11월 16일
Almost, because when I transpose I get a 60x10 matrix instead of 10x60 which is what i want.
But what i did to solve it is:
[M,N] = size(c);
d = zeros(N,M);
d(:) = c;
e = d'
c is the solution to your answer(without tranposing at the end) and e is exactly what I want
Thank you all for your help!!!!!!!!!!1
Sean de Wolski
Sean de Wolski 2011년 11월 16일
Good catch!
Just add this line will do the same thing.
sum4x4 = reshape(sum4x4,size(sum4x4,2),size(sum4x4,1))'
j_solar
j_solar 2011년 11월 17일
yeah, actually,right after I posted my answer I changed it to what you now say! It is more elegant. :)

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Fangjun Jiang
Fangjun Jiang 2011년 11월 15일

1 개 추천

Because the size of your matrix, you might want to re-use the matrix so replace the variable "e" and "f" with "d".
d=rand(40,24);
[M,N]=size(d);
e=mat2cell(d,4*ones(M/4,1),4*ones(N/4,1));
f=cellfun(@(x) sum(x(:)),e);

댓글 수: 1

j_solar
j_solar 2011년 11월 15일
Yeah!!!! That works really well. Finally used a smaller matrix, 40x240 and its 2.2 times faster than with a loop. For bigger matrices this difference will probably increase BIG TIME!!!

댓글을 달려면 로그인하십시오.

Sean de Wolski
Sean de Wolski 2011년 11월 15일

1 개 추천

How about blkproc if you have the Image Processing Toolbox (or blockproc in newer versions).
blkproc(magic(16),[4 4], @(x)(sum(x(:))))
I'm surprised conversion to cell is faster than a well done for-loop. Perhaps you could post what you had for the nested for-loops?

댓글 수: 4

j_solar
j_solar 2011년 11월 15일
This is what I had before, maybe not the best. With the change offered by fangjun the time decreased by 2.2
a = zeros(40, 240);
for j = 1:240
for k= 1:40
b(k,j) = sum(sum(a((k-1)*division_cel+1:k*division_cel,(j-1)*division_cel+1:j*division_cel)));
end
end
I have tried blkproc but that returns a 4x4 and I need to add the 4x4 inside returning in my case a 10x60
j_solar
j_solar 2011년 11월 15일
upps,thats not right. 40=10 and 240 =60 in my loop. and division_cel is 4
Sean de Wolski
Sean de Wolski 2011년 11월 15일
in blkproc, did you change magic(16) to 'a'? the magic(16) was for demo purposes only.
Sean de Wolski
Sean de Wolski 2011년 11월 15일
Your for-loop could be sped up a little by building the j-index vectors outside of the k for-loop. Overall it's good though!

댓글을 달려면 로그인하십시오.

Andrei Bobrov
Andrei Bobrov 2011년 11월 17일

0 개 추천

e.g.
A = randi(25,12,16);
m = 4;
n = 4;
[M,N] = size(A);
out = reshape(sum(sum(reshape(A.',n,N/n,m,[]),3)),n,[])';
or
out2 = reshape(sum(sum(reshape(A,m,M/m,n,[]),3)),[],n);
or2
out3 = blockproc(A,[m n],@(block_struct)sum(block_struct.data(:)));

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by