简体   繁体   English

Matlab矩阵中矩阵的平均值

[英]Mean values of a matrix in a matrix on Matlab

This is about matlab. 这是关于matlab的。 Let's say I have a matrix like this 假设我有一个像这样的矩阵

A = [1,2,3,4,5;6,7,8,9,10;11,12,13,14,15]‍

Now I want to know how to get a mean value of a small matrix in A . 现在我想知道如何在A中获得小矩阵的平均值。 Like a mean of the matrix located upper left side [1,2;6,7] 像位于左上方的矩阵的平均值[1,2;6,7]

The only way I could think of is cut out the part I want to get a value from like this 我唯一能想到的方法就是切掉我想从中获得价值的部分

X = A(1:2,:);
XY = X(:,1:2);

and mean the values column wise Mcol = mean(XY); 并按列Mcol = mean(XY); .

and finally get a mean of the part by meaning Mcol row-wise. 最后通过逐行表示Mcol来获得零件的均值。

Mrow = mean(Mcol,2);

I don't think this is a smart way to do this so it would be great if someone helps me make it smarter and faster. 我认为这不是明智的做法,因此,如果有人帮助我使其变得更聪明,更快捷,那将是很棒的。

Your procedure is correct. 您的程序是正确的。 Some small improvements are: 一些小的改进是:

  • Get XY using indexing in a single step: XY = A(1:2, 1:2) 只需一步即可使用索引获取XYXY = A(1:2, 1:2)
  • Replace the two calls to mean by a single one on the linearized submatrix: mean(XY(:)) . 线性子矩阵上的单个调用替换mean的两个调用: mean(XY(:))
  • Avoid creating XY . 避免创建XY In this case you can linearize using reshape as follows: mean(reshape(A(1:2, 1:2), 1, [])) . 在这种情况下,您可以使用reshape进行线性化,如下所示: mean(reshape(A(1:2, 1:2), 1, []))

If you want to do this for all overlapping submatrices , im2col from the Image Processing Toolbox may be handy: 如果要对所有重叠的子矩阵执行此操作, im2col “图像处理工具箱”中的im2col可能会很方便:

submatrix_size = [2 2];
A_sub = im2col(A, submatrix_size);

gives

A_sub =
     1     6     2     7     3     8     4     9
     6    11     7    12     8    13     9    14
     2     7     3     8     4     9     5    10
     7    12     8    13     9    14    10    15

that is, each column is one of the submatrices linearized. 也就是说,每一列都是线性化的子矩阵之一。 So now you only need mean(A_sub, 1) to get the means of all submatrices. 因此,现在您只需要mean(A_sub, 1)即可获得所有子矩阵的均值。

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

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