Wolfram ResearchPRODUCTSPURCHASEFOR USERSCOMPANYOUR SITES
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.

Documentation  / Mathematica / Built-in Functions  / Advanced Documentation / Linear Algebra / Matrix and Tensor Operations /

 

Element-wise Operations

If you want to operate on the elements of a matrix, you can do this easily with Mathematica. First, build a matrix of floating point numbers.

In[1]:=

Out[2]//MatrixForm=

Arithmetic operations applied to a matrix thread down to work on each element. Thus, if 5 is added to the matrix, the result is equivalent to adding 5 to each element.

In[3]:=

Out[3]=

Here, every element of the matrix is doubled.

In[4]:=

Out[4]=

Here, every element of the matrix is squared.

In[5]:=

Out[5]=

If one matrix is divided by another, the division is done element by element. If the dimensions of the two matrices do not agree, there is an error.

In[6]:=

Out[6]=

To apply the Sin function to every element, you apply Sin to the entire matrix.

In[7]:=

Out[7]=

If both of the arguments of an operation are matrices, the operation is carried out on corresponding elements.

In[8]:=

Out[8]=

Note that the dimensions of the two matrices have to be compatible.

In[9]:=

Out[9]=

If one of the arguments is a matrix and the other is a vector, the operation is carried out between rows of the matrix and elements of the vector.

In[10]:=

Out[10]=

Note that the multiplication of two matrices using the operator Times (typically this is entered by placing two arguments together) produces a matrix with elements that are products of the corresponding elements of the matrices. If you want to carry out matrix multiplication, this can be done with the function Dot and is described in the section Matrix Multiplication. An example of element-wise multiplication is shown in the following example.

In[11]:=

Out[13]=

Listability

If you want to apply your own function to each element in a matrix, you may do this by giving your function the attribute Listable. This function squares each element and divides the result by 3.

In[1]:=

In[3]:=

In[4]:=

Out[4]=

Of course, the function will still work with symbolic matrices.

In[5]:=

Out[5]=

More information on Mathematica programming can be found in The Mathematica Book.

Map

Instead of using listability you can use Map to apply a function to every element in a matrix.

In[1]:=

This says that the function f should be applied to every element in the matrix.

In[2]:=

Out[2]=

Here, a function that squares its argument and divides the result by 3 is applied to every element in the matrix.

In[3]:=

Out[3]=

More information on Mathematica programming can be found in The Mathematica Book.



Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information


 © 2008 Wolfram Research, Inc.  Terms of Use  Privacy Policy |
Sign up for our newsletter: