简体   繁体   English

矩阵各列的平均值

[英]Mean value of each column of a matrix

I have a 64 X 64 matrix that I need to find the column-wise mean values for. 我有一个64 X 64矩阵,我需要查找其列平均值。

However, instead of dividing by the total number of elements in each column (ie 64), I need to divide by the total number of non-zeros in the matrix. 但是,我需要除以矩阵中非零的总数,而不是除以每列中元素的总数(即64)。

I managed to get it to work for a single column as shown below. 我设法将其用于单个列,如下所示。 For reference, the function that generates my matrix is titled fmu2(i,j) . 作为参考,生成我的矩阵的函数名为fmu2(i,j)

q = 0;
for i = 1:64
    if fmu2(i,1) ~= 0;
        q = q + 1;
    end
end

for i = 1:64
    mv = (1/q).*sum(fmu2(i,1));
end

This works for generating the "mean" value of the first column. 这用于生成第一列的“平均值”值。 However, I'm having trouble looping this procedure so that I will get the mean for each column. 但是,我在循环执行此过程时遇到麻烦,因此我将获取每一列的均值。 I tried doing a nested for loop, but it just calculated the mean for the entire 64 X 64 matrix instead of one column at a time. 我尝试做一个嵌套的for循环,但是它只是计算整个64 X 64矩阵的平均值,而不是一次计算一列。 Here's what I tried: 这是我尝试过的:

q = 0;
for i = 1:64
    for j = 1:64
        if fmu2(i,j) ~= 0;
            q = q +1;
        end
    end
end

for i = 1:64
    for j = 1:64
        mv = (1/q).*sum(fmu2(i,j));
    end
end

Like I said, this just gave me one value for the entire matrix instead of 64 individual "means" for each column. 就像我说的那样,这给了我整个矩阵一个值,而不是每一列有64个单独的“均值”。 Any help would be appreciated. 任何帮助,将不胜感激。

For one thing, do not call the function that generates your matrix in each iteration of a loop. 一方面,不要在循环的每次迭代中调用生成矩阵的函数。 This is extremely inefficient and will cause major problems if your function is complex enough to have side effects. 如果您的功能过于复杂以至于产生副作用,这将非常低效,并且会导致严重的问题。 Store the return value in a variable once, and refer to that variable from then on. 将返回值存储在变量中一次,然后从此开始引用该变量。

Secondly, you do not need any loops here at all. 其次,这里根本不需要任何循环。 The total number of nonzeros is given by the nnz function (short for number of non-zeros). 非零总数由nnz函数给出(非零数目的缩写)。 The sum function accepts an optional dimension argument, so you can just tell it to sum along the columns instead of along the rows or the whole matrix. sum函数接受一个可选的Dimension参数,因此您可以告诉它沿列求和而不是沿行或整个矩阵求和。

m = fmu2(i,1)
averages = sum(m, 1) / nnz(m)

averages will be a 64-element array with an average for each column, since sum(m, 1) is a 64 element sum along each column and nnz(m) is a scalar. averages将是一个64元素的数组,每列具有平均值,因为sum(m, 1)是沿着每一列的64个元素之和,而nnz(m)是标量。

One of the great things about MATLAB is that it provides vectorized implementations of just about everything. MATLAB的一大优点是它提供了几乎所有内容的向量化实现。 If you do it right, you should almost never have to use an explicit loop to do any mathematical operations at all. 如果做对了,几乎应该永远不需要使用显式循环来进行任何数学运算。

If you want the column-wise mean of non-zero elements you can do the following 如果您想要非零元素的按列平均值,可以执行以下操作

m = randi([0,5], 5, 5); % some data
avg = sum(m,1) ./ sum(m~=0,1);

This is a column-wise sum of values, divided by the column-wise number of elements not equal to 0. The result is a row vector where each element is the average of the corresponding column in m . 这是列值的总和,除以不等于0的列元素数。结果是行向量,其中每个元素是m相应列的平均值。

Note this is very flexible, you could use any condition in place of ~=0 . 请注意,这非常灵活,您可以使用任何条件代替~=0

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

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