给出矩阵:
A = [1 2 3; 4 5 6; 7 8 9];
sum
编写一行MATLAB命令来求和
A
中的矩阵元素。1)
for j=1:3,
for i=j:3,
A(i,:) = A(i,:)+A(j+1,:)+A(j+2,:)
end
end
2)
sum(A)
这些是正确答案吗?我不知道如何使用if
,while
和for
。任何人都可以向我解释一下吗?
答案 0 :(得分:31)
对于使用sum(sum(A))
的非常大的矩阵,可能比sum(A(:))
更快:
>> A = rand(20000);
>> tic; B=sum(A(:)); toc; tic; C=sum(sum(A)); toc
Elapsed time is 0.407980 seconds.
Elapsed time is 0.322624 seconds.
答案 1 :(得分:18)
1)
total = 0;
for i=1:size(A,1)
for j=1:size(A,2)
total = total + A(i,j);
end
end
2)
total = sum(A(:));
答案 2 :(得分:10)
第一个问题的另一个答案是使用一个进行循环,并使用函数linear indexing对数组执行NUMEL以获取元素总数:
total = 0;
for i = 1:numel(A)
total = total+A(i);
end
答案 3 :(得分:3)
尽可能避免使用for循环。
sum(A(:))
很棒但是如果你有一些逻辑索引,你不能使用(:)但你可以写
% Sum all elements under 45 in the matrix
sum ( sum ( A *. ( A < 45 ) )
由于sum对列求和,并对由第一个和创建的行向量求和。请注意,这仅适用于矩阵为2维的情况。
答案 4 :(得分:2)
最佳做法是在Matlab中避免循环或递归。
在sum(A(:))
和sum(sum(A))
之间。
根据我的经验,Matlab中的数组似乎作为堆积列向量存储在内存中的连续块中。所以A的形状在sum()
中并不重要。 (可以测试reshape()
并检查Matlab中的重塑是否很快。如果是,那么我们有理由相信数组的形状与数据的存储和操作方式没有直接关系。)
因此,没有理由sum(sum(A))
应该更快。如果Matlab实际创建一个行向量,首先记录A的每一列的总和然后对列进行求和,这将会更慢。但我认为sum(sum(A))
在用户中非常广泛。他们可能会将sum(sum(A))
硬编码为单个循环,与sum(A(:))
相同。
下面我提供一些测试结果。在每个测试中,A = rand(大小)和大小在显示的文本中指定。
首先使用tic toc。
Size 100x100
sum(A(:))
Elapsed time is 0.000025 seconds.
sum(sum(A))
Elapsed time is 0.000018 seconds.
Size 10000x1
sum(A(:))
Elapsed time is 0.000014 seconds.
sum(A)
Elapsed time is 0.000013 seconds.
Size 1000x1000
sum(A(:))
Elapsed time is 0.001641 seconds.
sum(A)
Elapsed time is 0.001561 seconds.
Size 1000000
sum(A(:))
Elapsed time is 0.002439 seconds.
sum(A)
Elapsed time is 0.001697 seconds.
Size 10000x10000
sum(A(:))
Elapsed time is 0.148504 seconds.
sum(A)
Elapsed time is 0.155160 seconds.
Size 100000000
Error using rand
Out of memory. Type HELP MEMORY for your options.
Error in test27 (line 70)
A=rand(100000000,1);
下面是使用cputime
Size 100x100
The cputime for sum(A(:)) in seconds is
0
The cputime for sum(sum(A)) in seconds is
0
Size 10000x1
The cputime for sum(A(:)) in seconds is
0
The cputime for sum(sum(A)) in seconds is
0
Size 1000x1000
The cputime for sum(A(:)) in seconds is
0
The cputime for sum(sum(A)) in seconds is
0
Size 1000000
The cputime for sum(A(:)) in seconds is
0
The cputime for sum(sum(A)) in seconds is
0
Size 10000x10000
The cputime for sum(A(:)) in seconds is
0.312
The cputime for sum(sum(A)) in seconds is
0.312
Size 100000000
Error using rand
Out of memory. Type HELP MEMORY for your options.
Error in test27_2 (line 70)
A=rand(100000000,1);
根据我的经验,两个计时器只有.1s才有意义。因此,如果您对Matlab计时器有类似的经验,那么所有测试都无法识别sum(A(:))
和sum(sum(A))
。
我尝试了计算机上允许的最大尺寸。
Size 10000x10000
sum(A(:))
Elapsed time is 0.151256 seconds.
sum(A)
Elapsed time is 0.143937 seconds.
Size 10000x10000
sum(A(:))
Elapsed time is 0.149802 seconds.
sum(A)
Elapsed time is 0.145227 seconds.
Size 10000x10000
The cputime for sum(A(:)) in seconds is
0.2808
The cputime for sum(sum(A)) in seconds is
0.312
Size 10000x10000
The cputime for sum(A(:)) in seconds is
0.312
The cputime for sum(sum(A)) in seconds is
0.312
Size 10000x10000
The cputime for sum(A(:)) in seconds is
0.312
The cputime for sum(sum(A)) in seconds is
0.312
它们似乎相当。
要么一个人都好。但sum(sum(A))
要求您知道数组的维度为2。
答案 5 :(得分:0)
您正在尝试总结二维数组的所有元素
在Matlab中使用
Array_Sum = sum(sum(Array_Name));