简体   繁体   中英

Matlab - matrix addition in for loop

I have a 7x21 matrix called A. Within this matrix there are three equally sized 7x7 submatrices. I call them B,C and D, where B = A(:,1:7), C = A(:,8:14) and D = A(:,15:21).

How can I produce a matrix E which is also 7x7 matrix where simply B, C and D are added up, ie E = B+C+D.

Thanks a lot for your help!

I don't see what's going to be more straightforward and concise than

E = A(:,1:7) + A(:,8:14) + A(:,15:21)

Unless you need an expression that generalizes in some way you're not describing...

Generic code to get such an output -

N = 3; %// Number of submatrices
[m,n] = size(A) %// Get size [no. of cols must be multiple of N
E = reshape(sum(reshape(A,m*n/N,[]),2),m,n/N)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM