简体   繁体   English

Matlab-for循环中的矩阵加法

[英]Matlab - matrix addition in for loop

I have a 7x21 matrix called A. Within this matrix there are three equally sized 7x7 submatrices. 我有一个称为A的7x21矩阵。在此矩阵中,有三个大小相等的7x7子矩阵。 I call them B,C and D, where B = A(:,1:7), C = A(:,8:14) and D = A(:,15:21). 我称它们为B,C和D,其中B = A(:,1:7),C = A(:,8:14)和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. 如何生成矩阵E,它也是7x7矩阵,其中简单地将B,C和D相加,即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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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