Basic Input

  1. Remarks

  2. MATLAB is a matrix-oriented environment and programming language, in fact vectors and matrices are the basic data structures. Naming in MATLAB is case-sensitive (Pizza is different from pizza).

  3. Objects Names

  4. We can input new variables without preallocating new memory for them, that is, variable can be input directly,

    VariableName = Expression

    For instance, let’s try with an assignment statements where values are assigned to variables.

    x = 1
    y = x

    Note that,

    • for each variable must be assigned a value before it may be used on the right of an assignment statement,
    • assigning one variable to an other assigns the value of that variable, not the variable itself,
    • legal names consist of any combination of letters and digits, starting with a letter.
  5. Create Vectors and Matrices

  6. All data in MATLAB are matrices by construction, even if they are a scalar (1 by 1), K
    by 1 or 1 by K (vectors). Vectors, both row ( 1 by K) and column (K by 1)
    can be entered directly into the command window.

    The number of entries is known as the length of the vector and the entries are often referred
    to as elements or components of the vector.

    We use comma to enter elements by column and semicolon to enter elements by row, for matrices we use both while for row-vectors we use comma and semicolon for column-vectors. The entries must be enclosed in square brackets.
    Let’s try to create a row vector…

    >> V1 = [15, 7, 83]
    >> V1 = [15 7 83]

    …and a column vector.

    >> V2 = [15; 7; 83]

    As we stated before we can assign or insert not only elements but also functions in a vector or matrix,

    >> V3 = [15; 7; sqrt(83^2)]

    Then, insert a matrix by one row at time separating them with a semicolon (;) to insert the next row-vector.

    >> M1 = [1 2 3; 4 5 6; 7 8 9]

    Note that elements separated only from blank space are interpreted as row-vectors.

    The Empty Matrix

    It is a matrix with no elements, very useful for concatenation in cycles ore when we are calling some functions with multiple inputs but we don’t specify all, for example somefunction(x,[],y).

    >> x = []

    Extracting elements from vectors and matrices

    We can access to elements of a vectors vectorname(#) with a single index notations for vectors, for example given a row vector of 8th-elements,

    >> y = [ 1 2 4 8 16 32 64 128];

    we can get the 3rd to 6th entries,

    >> y(3:6)
    4 8 16 32

    or simply

    >> y(5)

    while in the matrix case, we have to use double indexing of matrices using the notation matrixname(#,#).

    >> x = [1 2 3; 4 5 6; 7 8 9]
    >> x(1,1)
    ans =
    >> x(2,3)
    ans =

    Furthermore, the colon operator (:) can be interpreted as all elements in that dimension, when we wish to select all the elements of a row or of a column.

    >> M = [1 2 3; 4 5 6; 7 8 9]
    >> M(:,1)
    ans =

    For instance, M(:,1), is interpreted as all elements from matrix M in column 1.
    We can convert a row vector into a column vector and vice-versa by transposition.

    >> M'
    ans =
         1     4     7
         2     5     8
         3     6     9
  7. Concatenation

  8. It is the process by which one vector or matrix is appended to another, both horizontal and vertical concatenation are possible.
    Suppose we have two matrix x and y 2 by 2,

    >> x = [1 2; 3 4];
    >> y = [5 6; 7 8];

    and we want to create z a matrix composed by x and y.
    We can create Z as a 4 by 2 matrix,

    >> z = [x; y];

    …or as a 2 by 4 matrix.

    >> z = [x y];

    Concatenating is simply using block-matrix forms in standard matrix algebra.

  9. Calling function

  10. The generic structure of a function call is

    [out1, out2, out3, . . .]=function(in1, in2, in3, . . .) 

    The important aspects of this structure are,

    • If only one output is needed, brackets ([ ]) are optional, for example y=mean(x).
    • If multiple outputs are required, the outputs must be encapsulated in brackets, such as in [y, index] = min(x).
    • The number of output variables determines how many outputs will be returned. Asking for more outputs than the function provides will generally result in an error.
    • Both inputs and outputs must be separated by commas (,).
    • Inputs can be the result of other functions as long as only the first output is requires (e.g. mean(var(x))).
    • Inputs can contain only vector or matrix or selected elements from them (e.g. mean(x([1 2] ,[1 2]))).

    For instance, the size function returns the dimension of an array,

    >> [m,n] = size(z)
    m =
    n =

    where the first output (m) refers to the row of the matrix while (n) to the columns.


  1. Create three row-vectors, one row-column and two matrices of these dimensions with elements that you prefer,
    • a, b (1 by 2) and c (1 by 5)
    • d (4 by 1)
    • e and f (3 by 2)
  2. Using the previous objects and concatenation function, create a square matrix M (5 by 5) [hint: use a step by step concatenation]
    This is a possible layout of the partitioned matrix M.
  3. Create N square submatrices from M that follow its diagonal.

M = [[a;e],d,[f;b];c]

%d bloggers like this: