English 中文(简体)
如何在MATLAB中汇总矩阵要素?
原标题:What are the ways to sum matrix elements in MATLAB?

鉴于汇总表:

A = [1 2 3; 4 5 6; 7 8 9];
  1. How could you use a for loop to compute the sum of the elements in the matrix?
  2. Write a one line MATLAB command using the function sum to sum the matrix elements in A.

My answer:

1 P-4, 1 P-3, 1 FS, 1 NS

for j=1:3,
    for i=j:3,
        A(i,:) = A(i,:)+A(j+1,:)+A(j+2,:)
    end
end

2)

sum(A)

这些答案是否正确? I don t 知道如何使用if ,whilefor。 谁能向我解释?

问题回答

使用<代码>(摘要(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 P-4, 1 P-3, 1 FS, 1 NS

total = 0;
for i=1:size(A,1 P-4, 1 P-3, 1 FS, 1 NS
  for j=1:size(A,2)
    total = total + A(i,j);
  end
end

2)

total = sum(A(:));

第一个问题的另一个答案是将

total = 0;
for i = 1:numel(A)
  total = total+A(i);
end

尽可能避免休息。

sum(A(:))

然而,如果你能够用某种逻辑的指数来计算,那么你就可以使用(......)但可以书写:

% Sum all elements under 45 in the matrix
sum ( sum ( A *. ( A < 45 ) )

由于总和将头一笔款项所创造的浏览矢量汇总起来。 请注意,只有在矩阵为2-dim的情况下,才会做到这一点。

最佳做法无疑是避免马特拉布的漏洞或休妻。

Between sum(A(:)) and sum(sum(A)). In my experience, arrays in Matlab seems to be stored in a continuous block in memory as stacked column vectors. So the shape of A does not quite matter in sum(). (One can test reshape() and check if reshaping is fast in Matlab. If it is, then we have a reason to believe that the shape of an array is not directly related to the way the data is stored and manipulated.)

因此,没有理由加快<代码>(摘要(A)。 如果Matlab实际上制造了一种滚动矢量,记录A一栏中每一栏的总和,然后比各栏中的总和,则会放缓。 但我想sum((sum(A))在用户中广为传播。 硬编码<代码>sum(((A))可能是一种单一形式,与<代码>sum(A(:)相同。

下面我提供一些检测结果。 在每次测试中,A=rand(size)和大小在显示的文本中都有具体规定。

首先是使用斜线。

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);

下面是使用假肢

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);

在我的经验中,两个时间都只是有意义的。 因此,如果你在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

They seem equivalent. Either one is good. But sum(sum(A)) requires that you know the dimension of your array is 2.

你正在努力总结2-D Array的全部内容。

在Matlab使用

Array_Sum = (sum(Array_Name);





相关问题
Matrix to Represent a Triangle in Screen Space

So i have a set of four points in 3D Space. P1 [0, 0, 0] P2 [128, 0, 0] P3 [0, 128, 0] P4 [128, 128, 0] Which I m then projecting orthographically to the screen effectively giving me two ...

Multiply a 3D matrix with a 2D matrix

Suppose I have an AxBxC matrix X and a BxD matrix Y. Is there a non-loop method by which I can multiply each of the C AxB matrices with Y?

matrix and vector template classes in c++

#include <array> template <typename T> class Vector4<T> { std::array<T, 4> _a; // or T _a[4]; ? }; template <typename T> class Matrix4<T> { std::array<...

Linear Independence Matrix

Suppose we have a m by n matrix A with rank m and a set K⊆{1..n} such that the columns of A indexed by K are linearly independent. Now we want to extend K and find a set L so that k⊆L and columns ...

Difference between MATLAB s matrix notations

How do you read the following MATLAB codes? #1 K>> [p,d]=eig(A) // Not sure about the syntax. p = 0.5257 -0.8507 -0.8507 -0.5257 d = ...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Java large datastructure for storing a matrix

I need to store a 2d matrix containing zip codes and the distance in km between each one of them. My client has an application that calculates the distances which are then stored in an Excel file. ...

Checking row and column for a word in python

I am trying to create a checking program to see if the word is in a matrix horizontally or vertically. I have the code for checking the row, but would checking the column be similar to the row code? ...

热门标签