matlab - 如何确定最后一个的平均值matlab矩阵每列100个非零数

标签 matlab matrix mean

我想计算最后一个的平均值,例如matlab中矩阵的每一列中的3个非零数字。这些列的末尾用零填充以创建相同长度的向量。

示例矩阵:

A = [5 6 3 5 6 8 9;
     1 2 3 5 4 7 6;
     0 1 2 3 4 5 6; 
     0 0 1 2 3 4 5; 
     0 0 0 1 2 3 4; 
     0 0 0 0 2 3 4;
     0 0 0 0 2 3 4; 
     0 0 0 0 0 0 3]

最佳答案

可能有更有效的解决方案,但一种方法是使用 sum 来查找给定列中非零行的数量。然后通过使用 arrayfun 遍历所有列并对列中零之前的 N 行进行平均来获取 A 的平均值。

%// Number of elements to average
N = 3;

%// Last non-zero row in each column
lastrow = sum(A ~= 0, 1);

%// Ensure that we don't have any indices less than 1
startrow = max(lastrow - N + 1, 1);

%// Compute the mean for each column using the specified rows
means = arrayfun(@(k)mean(A(startrow(k):lastrow(k),k)), 1:size(A, 2));

示例

对于您的示例数据,这将产生:

3.0000    3.0000    2.0000    2.0000    2.0000    3.0000    3.6667

更新:另一种选择

另一种方法是使用卷积来实际为您解决这个问题。您可以使用卷积核计算均值。如果您想要一个矩阵的所有 3 行组合的平均值,您的内核将是:

kernel = [1; 1; 1] ./ 3;

当与感兴趣的矩阵进行卷积时,这将计算输入矩阵中所有 3 行组合的平均值。

B = [1 2 3;
     4 5 6;
     7 8 9];

conv2(B, kernel)

    0.3333    0.6667    1.0000
    1.6667    2.3333    3.0000
    4.0000    5.0000    6.0000
    3.6667    4.3333    5.0000
    2.3333    2.6667    3.0000

在下面的示例中,我执行此操作然后仅返回我们关心的区域的值(其中平均值仅由每列中的最后一个 N 非零值组成)

%// Find the last non-zero entry in each column
lastrow = sum(A ~= 0, 1);

%// Use convolution to compute the mean for every N rows
%// This will be applied to ALL of A
convmean = conv2(A, ones(N, 1)./N);

%// Select only the means that we care about
%// Because of the padding of CONV2, these will live at the rows
%// stored in LASTROW
means = convmean(sub2ind(size(convmean), lastrow, 1:size(A, 2)));

%// Now correct for cases where fewer than N samples were averaged
means = (means * N) ./ min(lastrow, N);

同样,输出是一样的

3.0000    3.0000    2.0000    2.0000    2.0000    3.0000    3.6667

比较

我运行了一个快速测试脚本来比较这两种方法的性能。很明显,基于卷积的方法要快得多。

enter image description here

这是完整的测试脚本。

function benchmark()
    dims = round(linspace(1, 1000, 100));

    times1 = zeros(size(dims));
    times2 = zeros(size(dims));

    N = 3;

    for k = 1:numel(dims)
        A = triu(rand(dims(k)));
        times1(k) = timeit(@()test_arrayfun(N, A));
        A = triu(rand(dims(k)));
        times2(k) = timeit(@()test_convolution(N, A));
    end

    figure;
    plot(dims, times1);
    hold on
    plot(dims, times2);

    legend({'arrayfun', 'convolution'})
    xlabel('Dimension of A')
    ylabel('Execution Time (seconds)')
end

function test_arrayfun(N, A)
    %// Last non-zero row in each column
    lastrow = sum(A ~= 0, 1);

    %// Ensure that we don't have any indices less than 1
    startrow = max(lastrow - N + 1, 1);

    %// Compute the mean for each column using the specified rows
    means = arrayfun(@(k)mean(A(startrow(k):lastrow(k),k)), 1:size(A, 2));
end

function test_convolution(N, A)
    %// Find the last non-zero entry in each column
    lastrow = sum(A ~= 0, 1);

    %// Use convolution to compute the mean for every N rows
    %// This will be applied to ALL of A
    convmean = conv2(A, ones(N, 1)./N);

    %// Select only the means that we care about
    %// Because of the padding of CONV2, these will live at the rows
    %// stored in LASTROW
    means = convmean(sub2ind(size(convmean), lastrow, 1:size(A, 2)));

    %// Now correct for cases where fewer than N samples were averaged
    means = (means * N) ./ min(lastrow, N);
end

关于matlab - 如何确定最后一个的平均值matlab矩阵每列100个非零数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36288101/

相关文章:

r - 如何将矩阵的每一行除以R中向量的元素

python - 如何在 Python 中生成两个矩阵的可用张量积

matlab - 如何在MatConvNet中使用使用cnn_mnist示例训练的网络?

matlab - MATLAB 是否有任何显示向量和矩阵维度的 Debug模式?

c++ - 无效的 Mex 函数

python - 如何使用 NumPY 对缩小函数进行矢量化?

pandas - pandas groupby 中的自定义聚合函数

r - 取矩阵 r 中行的平均值

r - 如何在R中的Boxplot中绘制均值和标准误差

matlab - 来自元胞数组的结构字段