我被困在如何检查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变量没有更新...它只是保持为零。感谢任何帮助,并解释为什么你做了你所做的事情,因为我想学习。
答案 0 :(得分: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。
答案 1 :(得分:4)
对于这个问题,你不需要使用显式循环(它也很慢)。
如果RandomNumbers > 25
中的对应元素大于25,则 RandomNumbers
将返回一个矩阵,其中每个元素为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))
它也快得多。
答案 2 :(得分: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
我希望这有帮助!