Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

Matrix Manipulation

This example shows how to do some basic matrix manipulations in MATLAB®.

We start by creating a magic square and assigning it to the variable A.

A = magic(3)
A = 

     8     1     6
     3     5     7
     4     9     2

Here's how to add 2 to each element of A.

Note that MATLAB requires no special handling of matrix math.

A+2
ans = 

    10     3     8
     5     7     9
     6    11     4

The apostrophe symbol denotes the complex conjugate transpose of a matrix.

Here's how to take the transpose of A.

A'
ans = 

     8     3     4
     1     5     9
     6     7     2

The symbol * denotes multiplication of matrices.

Let's create a new matrix B and multiply A by B.

B = 2*ones(3)
B = 

     2     2     2
     2     2     2
     2     2     2

A*B
ans = 

    30    30    30
    30    30    30
    30    30    30

We can also multiply each element of A with its corresponding element of B by using the .* operator.

A.*B
ans = 

    16     2    12
     6    10    14
     8    18     4

MATLAB has functions for nearly every type of common matrix calculation. For example, we can find the eigenvalues of A using the "eig" command.

eig(A)
ans = 

   15.0000
    4.8990
   -4.8990

This concludes our brief tour of some MATLAB matrix handling capabilities.


Was this topic helpful?