MATLAB Variable as Scalar, Vector, or Matrix

After reading the MATLAB Variable as Scalar, Vector, or Matrix topic, you will understand how to create a variable as a Scalar, Vector, and Matrix in MATLAB.

Variable in MATLAB can be of following types:

  • A variable (or array) with one element is called scalar.
  • A variable (or array) with one row or one column of elements called vector.
  • A variable (or array) with elements in rows and columns is called matrix.

MATLAB Variable as a Scalar

Aim (1): To define variable x having element 2 as a scalar.

Program (1):

x = 2

Output (1):

x = 2

MATLAB VIEW – Program (1) and Output (1):

Type the following code in the command window and see the result –

MATLAB Variable as Vector

Aim (1): To define variable x as a vector and the elements of x are given by x=(1, 22, 3).

Program (1):

x = [1,22,3]

Output (1):

x = 1   22     3

MATLAB VIEW – Program (1) and Output (1):

Type the following code in the command window and see the result –

MATLAB – Variable as Matrix

Aim (1): To define variable x as a matrix and the elements of x are given by: x=(1, 22, 3, 2, 34, 4, 12, 2, 4).

Program (1):

x = [1,22,3;2,34,4;12,2,4]

Output (1):

x =
1   22     3
2   34     4
12   2     4

MATLAB VIEW – Program (1) and Output (1):

Type the following code in the command window and see the result–

MATLAB – Proper commands for managing variables

Commands like clear, who and whos, that can be used to eliminate variables or to obtain information about variables that have been created. When these commands typed in the Command Window and the Enter key is pressed, either they provide information, or they perform a task as specified below.

MATLAB – who command

who displays a list of the variables currently in the memory.

MATLAB VIEW – to show who command execution

Before executing who command, variables are listed in Workspace as shown below

After executing who command, variables name displayed in the command window as shown below

MATLAB whos command

whos displays a list of the variables currently in the memory and their sizes together with information about their bytes and class.

MATLAB VIEW – to show whos command execution

Before executing whos command, variables are listed in Workspace as shown below

After executing the whos command, variables name, size, bytes, class, attributes displayed in the command window as shown below

MATLAB clear command

In MATLAB, clear command clears all the variables of the workspace.

MATLAB VIEW – to show clear command execution

Before executing the clear command, variables are listed in Workspace as shown below

After executing the clear command, variables gets cleared from the Workspace as shown below

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *