MATLAB Interview Questions and Answers

MATLAB Tutorial – Learn MATLAB Online

The name MATLAB stands for MATrix LABoratory. MATLAB was written originally to provide easy access to matrix software developed by the LINPACK (linear system package) and EISPACK (Eigen system package) projects. The below MATLAB tutorial topics are designed to get you started quickly in MATLAB. The topics are intended to make you familiar with the basics of MATLAB.

MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming environment. Furthermore, MATLAB is a modern programming language environment: it has sophisticated data structures, contains built-in editing and debugging tools, and supports object-oriented programming. These factors make MATLAB an excellent tool for teaching and research.

MATLAB Tutorial For Beginners

Introduction

MATLAB has many advantages compared to conventional computer languages (e.g., C, FORTRAN) for solving technical problems. MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. The software package has been commercially available since 1984 and is now considered as a standard tool at most universities and industries worldwide.

MATLAB Basic

As an example of a simple interactive calculation, just type the expression you want to evaluate. Let’s start at the very beginning. For example, let’s suppose you want to calculate the expression, 1 + 2 £ 3. You type it at the prompt command (>>) as follows,

>> 1+2*3

ans = 7

You will have noticed that if you do not specify an output variable, MATLAB uses a default variable ans, short for answer, to store the results of the current calculation. Note that the variable ans is created (or overwritten, if it is already existed). To avoid this, you may assign a value to a variable or output argument name. For example,

>> x = 1+2*3

x  7

will result in x being given the value 1 + 2 £ 3 = 7. This variable name can always be used to refer to the results of the previous computations. Therefore, computing 4x will result in

>> 4*x

ans = 28.0000

MATLAB variables

MATLAB variables are created with an assignment statement. The syntax of variable assignment is variable name = a value (or an expression), For example,

>> x = expression

where expression is a combination of numerical values, mathematical operators, variables, and function calls. Once a variable has been created, it can be reassigned. In addition, if you do not wish to see the intermediate results, you can suppress the numerical output by putting a semicolon (;) at the end of the line. Then the sequence of commands looks like this:

>> t = 5;
>> t = t+1
t =
6

Error messages

If we enter an expression incorrectly, MATLAB will return an error message. For example, in the following, we left out the multiplication sign, *, in the following expression

>> x = 10;
>> 5x
??? 5x

Error: Unexpected MATLAB expression.

MATLAB Commands 

To view the online documentation, select MATLAB Help from Help menu or MATLAB Help directly in the Command Window. The preferred method is to use the Help Browser. The Help Browser can be started by selecting the? icon from the desktop toolbar. On the other hand, information about any command is available by typing

>> help Command

Another way to get help is to use the lookfor command. The lookfor command differs from the help command. The help command searches for an exact function name match, while the lookfor command searches the quick summary information in each function for a match.

For example, suppose that we were looking for a function to take the inverse of a matrix. Since MATLAB does not have a function named inverse, the command help inverse will produce nothing. On the other hand, the command lookfor inverse will produce detailed information, which includes the function of interest, inv.

>> lookfor inverse

Use on-line help to request info on a specific function

>> help sqrt

In the current version (MATLAB version 7), the doc function opens the on-line version of the help manual. This is very helpful for more complex commands.

>> doc plot

Use lookfor to fnd functions by keywords. The general form is

> lookfor FunctionName

MATLAB Plotting

MATLAB has an excellent set of graphic tools. MATLAB Plotting a given data set or the results of computation is possible with very few commands. You are highly encouraged to plot mathematical functions and results of analysis as often as possible. Trying to understand mathematical equations with graphics is an enjoyable.

How to Create Simple Plot?

The basic MATLAB graphing procedure, for example in 2D, is to take a vector of x-coordinates, x = (x1; : : : ; xN), and a vector of y-coordinates, y = (y1; : : : ; yN), locate the points (xi; yi), with i = 1; 2; : : : ; n and then join them by straight lines. You need to prepare x and y in an identical array form; namely, x and y are both row arrays or column arrays of the same length.

The MATLAB command to plot a graph is plot(x,y). The vectors x = (1; 2; 3; 4; 5; 6)

and y = (3;¡1; 2; 4; 5; 1)
>> x = [1 2 3 4 5 6];
y = [3 -1 2

MATLAB Matrices

Matrices are the basic elements of the MATLAB environment. A matrix is a two-dimensional array consisting of m rows and n columns. Special cases are column vectors (n = 1) and row vectors (m = 1). In this below will illustrate how to apply different operations on matrices.

MATLAB Array Operations

MATLAB has two different types of arithmetic operations: matrix arithmetic operations and array arithmetic operations. We have seen matrix arithmetic operations in the previous lab. Now, we are interested in array operations.

Matrix Arithmetic Operations

As we mentioned earlier, MATLAB allows arithmetic operations: +, ¡, ¤, and ^ to be carried out on matrices.


A+B or B+A is valid if A and B are of the same size
A*B is valid if A's number of column equals B's number of rows
A^2 is valid if A is square and equals A*A
®*A or A*® multiplies each element of A by ®

Array Arithmetic Operations

Array arithmetic operations or array operations for short, are done element-by-element. The period character, ., distinguishes the array operations from the matrix operations. However, since the matrix and array operations are the same for addition (+) and subtraction (¡), the character pairs (:+) and (:¡) are not used.

MATLAB – Control Flow

MATLAB has some decision making structures for control of command execution. These decision making or control °ow structures include for loops, while loops, and if-else-end constructions. ControlFlow structures are often used in script M-Files and function M-Files. By creating a file with the extension .m, we can easily write and run programs. We do not need to compile the program since MATLAB is an interpretative (not compiled) language.

MATLAB – Operators

MATLAB has four controlFow structures: the if statement, the for loop, the while loop,and the switch statement.We can build expressions that use any combination of arithmetic, relational, and logical operators. Precedence rules determine the order in which MATLAB evaluates an expression.

  • 1 Parentheses ()
  • 2 Transpose (: 0), power (.^), matrix power (^)
  • 3 Unary plus (+), unary minus (¡), logical negation (»)
  • 4 Multiplication (: ¤), right division (: =), left division (:n),
  • matrix multiplication (¤), matrix right division (=),
  • matrix left division (n)
  • 5 Addition (+), subtraction (¡)
  • 6 Colon operator (:)
  • 7 Less than (<), less than or equal to (·), greater (>),
  • greater than or equal to (¸), equal to (==), not equal to (»=)
  • 8 Element-wise AND, (&)
  • 9 Element-wise OR, (j)

MATLAB Tutorial for beginners explain what MATLAB means, and it gives a brief understanding and important MATLAB concepts are explained above post. I will be adding more posts in MATLAB tutorial, so please bookmark the post for future reference too.