1

我被困在如何检查一个 100x100 矩阵中有多少个数字大于 25。下面是我到目前为止的代码:

loop = 1:100;
RandomNumbers = normrnd(0, 25, [100, 100]);
NumberCounter = 0;
for i = 1:10000
    if i >= 1
        if (RandomNumbers(loop, 100) > 25)
            NumberCounter = NumberCounter + 1
        elseif (RandomNumbers(100, loop) > 25)
            NumberCounter = NumberCounter + 1
        end
    end
end

我的 NumberCounter 变量没有更新......它只是保持为零。感谢您提供任何帮助,并解释您为什么这样做是因为我想学习。

4

3 回答 3

6

首先,让我用它在做什么来注释你的代码:

% This creates a list of numbers, 1 through 100 inclusive
loop = 1:100;
% This generates a 100x100 random matrix drawn from a normal distribution
% with mean 0 and standard deviation 25
RandomNumbers = normrnd(0, 25, [100, 100]);
NumberCounter = 0;
for i = 1:10000
    % This loop only runs over i from 1 to 10000, so i>=1 is always true.
    % This if statement is unnecessary.
    if i >= 1
        % Remember that loop is a _list_ of numbers: RandomNumbers(loop, 100)
        % is the whole 100th column of your random matrix, and so
        % RandomNumbers(loop, 100)>25 is a _list_ of 100 boolean values, 
        % corresponding to whether each element of the 100th column of your matrix
        % is greater than 25. By default, Matlab only treats a list of values as 
        % true if they are _all_ true, so this if-statement almost never evaluates
        % to true.
        if (RandomNumbers(loop, 100) > 25)
            NumberCounter = NumberCounter + 1
        % This test is doing the same thing, but testing the 100th row, instead of
        % the 100th column.
        elseif (RandomNumbers(100, loop) > 25)
            NumberCounter = NumberCounter + 1
        end
    end
end

您尝试执行的正确代码是:

RandomNumbers = normrnd(0, 25, [100, 100]);
NumberCounter = 0;
for i = 1:size(RandomNumbers,1)
    for j = 1:size(RandomNumbers,2)
        if RandomNumbers(i,j) > 25
            NumberCounter = NumberCounter + 1;
        end
    end
end

让我还要提一下,一种更快、更简洁的方法来做你想做的事情如下:

RandomNumbers = normrnd(0, 25, [100, 100]);
flatVersion = RandomNumbers(:);
NumberCounter = sum(flatVersion > 25);

之所以有效,是因为RandomNumbers(:)将矩阵展开为单个向量,并且因为sum每个真值计数为 1,每个假值计数为 0。

于 2012-11-10T06:51:51.933 回答
4

对于这个问题,您不需要使用显式循环(它也很慢)。

RandomNumbers > 25RandomNumbers如果对应的元素大于 25,则返回一个矩阵,其中每个元素为 1,否则为 0:

ans =
 Columns 1 to 34
 0 0 1 1 1 1 1 0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 1 0 1 0 0 0
 0 0 0 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 0 0 0
 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1 0 0 1 0 1 0 1
 1 0 1 1 1 0 1 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 0 1 1 1 1 0 0 0 0 0 1 0
 1 0 1 0 0 1 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 1 1 0 1 1 1 1 1
 1 0 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 0 1 0 1 0 0 1 1 1 1 1 1 0 1 0 0 1

 .
 .
 .

所需的结果是该矩阵的所有元素的总和。sum(RandomNumbers > 25)将返回矩阵中每一列的总和:

ans =
 Columns 1 to 26
 45 47 55 54 57 52 55 50 57 52 53 47 53 46 51 49 49 42 50 52 54 37 45 48 53 48
 Columns 27 to 52
 51 50 51 53 49 49 48 43 49 49 53 51 52 45 54 49 53 54 48 48 46 46 49 52 47 52
 Columns 53 to 78
 45 44 43 54 50 49 38 50 54 48 50 39 53 46 54 51 53 49 47 46 44 43 48 56 51 44
 Columns 79 to 100
 47 51 58 58 55 41 49 49 43 48 45 52 52 43 54 51 48 55 54 55 44 47

在这个向量上应用 sum 是我们想要的结果。因此,要检查 100x100 矩阵中有多少数大于 25,只需使用:

sum(sum(RandomNumbers > 25))

它也快得多。

于 2014-03-11T21:19:57.453 回答
1

您正在循环遍历第 100 行的一行和第 100 列的一列。您不会遍历矩阵中的所有元素。您需要有一个嵌套for循环。一个越过行,另一个越过行。

例如:

for i = 1:100
    for j = 1:100
        if i >= 1
            if (RandomNumbers(loop(i), loop(j)) > 25)
                NumberCounter = NumberCounter + 1
            end
        end
    end
end

我希望这有帮助!

于 2012-11-10T06:43:15.500 回答