zaro

How to Take an Array as an Input in MATLAB?

Published in MATLAB Array Input Indexing 4 mins read

Taking an array as input in MATLAB involves various methods, depending on whether you are defining it directly in your code, loading it from a file, or prompting a user for input. Once an array is "input" into MATLAB and assigned to a variable, you can then easily access and manipulate its individual elements or sections using indexing.

Methods for Inputting Arrays

There are several common ways to get array data into your MATLAB environment:

1. Manually Defining Arrays in Code

The most straightforward method for small arrays is to define them directly in your script or command window using square brackets [].

  • Row Vector: Elements are separated by spaces or commas.
    myRowVector = [1 2 3 4 5];
    % Or
    myRowVector = [1, 2, 3, 4, 5];
  • Column Vector: Elements are separated by semicolons.
    myColVector = [10; 20; 30; 40; 50];
  • Matrix (2D Array): Use spaces or commas for columns within a row, and semicolons to separate rows.
    myMatrix = [1 2 3; 4 5 6; 7 8 9];

2. Reading Arrays from Files

For larger datasets, arrays are typically loaded from external files. MATLAB supports various file formats.

  • .mat files: MATLAB's native format, ideal for saving and loading variables (including arrays) with full precision. Use load('filename.mat').
  • Text files (.txt, .csv): Use functions like readmatrix, readtable, csvread (older), or dlmread (older) depending on the file structure and data types.
    % Example using readmatrix for a CSV file
    dataArray = readmatrix('mydata.csv');

3. Getting User Input for Arrays

You can prompt the user to enter array values while a script is running using the input function. This is suitable for interactive scripts but less common for large arrays.

% Prompt user to enter a row vector
userArray = input('Enter a row vector within square brackets, e.g., [1 2 3]: ');

Note: The user must enter the input in a valid MATLAB array format.

Accessing Array Elements Using Indexing

Once you have an array "input" into MATLAB (i.e., assigned to a variable), you can access its contents using indexing. This is where the concept from the reference comes into play: you can specify elements of an array by simple row and column indexing.

MATLAB uses 1-based indexing, meaning the first element is at index 1, not 0.

1. Accessing Single Elements

To get the value of a specific element, you specify its row and column index in parentheses ().

Syntax Description Example (for myMatrix = [1 2 3; 4 5 6; 7 8 9];) Result
A(row, col) Accesses the element at the specified row and column. myMatrix(1, 2) 2
myMatrix(3, 1) 7

Reference Point: "Here is the element of A in the first row second column." This corresponds to A(1, 2).

2. Accessing Sections of an Array (Ranges)

You can access a block of elements by specifying ranges of rows and columns using the colon operator :. The colon : used by itself within an index means "all elements" along that dimension.

Syntax Description Example (for myMatrix) Result
A(row, col_range) Accesses elements in a specific row across a range of columns. myMatrix(1, 1:2) [1 2]
A(row_range, col) Accesses elements in a specific column across a range of rows. myMatrix(1:2, 3) [3; 6]
A(row_range, col_range) Accesses a submatrix. myMatrix(2:3, 1:2) [4 5; 7 8]
A(:, col_range) Accesses all rows in a range of columns. myMatrix(:, 2) [2; 5; 8]
A(row_range, :) Accesses a range of rows across all columns. myMatrix(3, :) [7 8 9]

Reference Point: "You can specify a range of rows and columns to access sections of an array such as row 1, columns 1 through 2." This corresponds to A(1, 1:2).

3. Accessing Non-Contiguous Elements

You can also access specific, non-adjacent elements or rows/columns by providing a vector of indices in the parentheses.

Syntax Description Example (for myMatrix) Result
A(row, [col1 col2 ...]) Accesses specific columns in a single row. myMatrix(1, [1 3]) [1 3]
A([row1 row2 ...], col) Accesses specific rows in a single column. myMatrix([1 3], 2) [2; 8]
A([row1 row2 ...], [col1 col2 ...]) Accesses elements at the intersections of specified rows and columns. myMatrix([1 3], [1 3]) [1 9]

Reference Point: "The elements do not have to be contiguous, such as row 1, columns 1 and 3." This corresponds to A(1, [1 3]).

In summary, taking an array as input can mean creating it manually, loading it from a file, or getting it from user input. Regardless of the method used to get the array into MATLAB, the process of working with its contents relies heavily on indexing to access specific elements or sections, as described in the reference.