# MATLAB Element by Element Operations

In the previous basic MATLAB tutorial post, MATLAB vectors were introduced. With this knowledge, we can now discuss how we can use vectors to perform MATLAB element by element operations, or element wise calculations.

In most other programming languages, to perform a process repeatedly requires the use of a loop. This could be performing some complex operation or simply evaluating an equation for a number of different values. In MATLAB, we handle repetitive steps a bit differently - using what we call *element by element* operations on a vector.

### MATLAB Element by element Calculations

When doing MATLAB element by element operations, rather than explicitly iterating in a loop, MATLAB will repeat a process or evaluation on each element in a vector automatically. Of course, this only happens if we specify the element by element math with the proper syntax!

This is best demonstrated with an example. Assume that you are told the height of a ball thrown from a roof is described by the equation: What is the height of the ball at every second between zero and 10?

To solve this in MATLAB, we first define a vector of time values:

>> t = [1:10] t = 0 1 2 3 4 5 6 7 8 9 10

Next, we type in the equation for the height of the ball with some special syntax to indicate we want MATLAB to perform the evaluation element-by-element. Before any multiplication, division, or exponent we include a dot (.) to specify this. In other words, we are telling MATLAB to calculate the height of the ball for every value in the vector of time t and return to us a vector of the answers:

>> y = 1000+70.*t-16.1.*t.^2 y = 1.0e+03 * Columns 1 through 6 1.0000 1.0539 1.0756 1.0651 1.0224 0.9475 Columns 7 through 11 0.8404 0.7011 0.5296 0.3259 0.0900

Note the funny output code. Something similar to this will occur if your command window is not wide enough to display the contents of the entire vector on a single line. It is telling you that you have a row vector with the values in the specified columns.

In General, if you want to perform element-by-element calculations with vectors, you should use a dot (.) before any multiplication (*), division (/), or power (^) operators that involve vectors. I usually advise new users of MATLAB to go ahead and use the dot if they are in doubt if its required. The instances when you would NOT want to use the dot are when you want to do matrix multiplication, which we will discuss in a later post.

For the next tutorial part I would like to switch gears a little bit and introduce a different topic that is very important in engineering: Basic MATLAB Plots!