Skip to main content
Engineering LibreTexts

4.2: Methods to Create Vectors in MATLAB

  • Page ID
    84394
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    By Carey A. Smith

    Example \(\PageIndex{1}\) Methods to create vectors in MATLAB

    The following code is aloso in the attached file "Methods_to_create_vectors_in_Matlab.mlx".

    %% Methods_to_create_vectors_in_Matlab.mlx
    % Vectors can be created in Matlab in the following ways.

    %% 1. Write a list of values between square brackets:
    A = [ 0, 1, 4, 9, 25] % The commas are optional
    B = [-3.1 1.789 2/7, exp(2)] % The values can be integers, decimals, or expressions.

    %% 2. Use the colon operator to automatically create a vector of equally spaced values:
    C = 1:10 % = [1 2 3 4 5 6 7 8 9 10]
    % v = n1:n2 creates a vector from n1 to n2
    % with an increment of 1
    % An “increment” can define the spacing.
    % The increment goes in the middle.
    D = 0 : 2 : 10 % [0 2 4 6 8 10]
    % The increment can be a fraction or a negative number:
    E = 2 : 0.5 : 4 % [2 2.5 3 3.5 4]
    F = 5 : -1 : 1 % [5 4 3 2 1]

    % Alternate method: Multiply by a scale factor:
    % Use the factor that multiplying a vector by a scalar
    % multiplies each element of the vector by that factor.
    theta = (0:(1/6):1) * pi
    % theta = [0 0.5236 1.0472 1.5708 2.0944 2.618 3.1416]

    %% 3. Derive a vector from another vector
    v1 = 11:15 % [11 12 13 14 15]
    % Ceate the squares of each element of v1
    v2 = v1.^2 % [121 144 169 196 225]
    % This uses the element-wise dot operator .^

    %% 4. Concatenate vectors (merge 2 or more vectors into 1 vector)
    name1 = 'Angela'
    name2 = 'Merkel'
    name12 = [name1,name2] % 'AngelaMerkel'
    % You can add a space between the names:
    name1_2 = [name1,' ',name2] % 'Angela Merkel'

    %% 5. Use the zeros() function
    G = zeros(1,4) % [0 0 0 0] 1 row, 4 columns
    % The format is zeros(nrows, ncolumns)
    % This is used to initialize a vector before a for loop

    % 6. Use the ones() function
    H = ones(1,4) % [1 1 1 1] 1 row, 4 columns
    J = 3*ones(1,4) % [3 3 3 3] 1 row, 4 columns
    % The format is ones(nrows, ncolumns)

    % 7. Use the linspace command
    K = linspace(1,10,5) % [1.00 3.25 5.50 7.75 10.00]
    % This creates a vector of 5 evenly spaced values between 1 and 10

    % 8. Use the logspace command
    L = logspace(0,3,7) % 1.0E3*[0.001 0.0032 0.010 0.0316 0.100 0.3162 1.00]
    % logspace(X1, X2, N) generates a row vector of N logarithmically
    % equally spaced points between 10^X1 and 10^X2.

    Solution

    Add example text here.

    Arreglos vectores - 5 - Octave

    https://www.youtube.com/watch?v=H-5cicDVr5U&list=PLM-p96nOrGcYsb5oML9JunGDgduyt-0gb&index=5  


    This page titled 4.2: Methods to Create Vectors in MATLAB is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.