0% found this document useful (0 votes)
17 views20 pages

Chapter 2

This document discusses arrays in MATLAB. It covers: 1) Arrays store and manipulate data as lists of numbers arranged in rows and columns. One-dimensional arrays are lists in a single row or column called vectors. Two-dimensional arrays are lists arranged in multiple rows and columns called matrices. 2) Vectors are created using square brackets with numbers separated by spaces/commas. Matrices are created using square brackets with each row in semicolon-separated lists. Functions like zeros, ones, and eye can generate arrays of all zeros, ones, or identities. 3) Individual elements in arrays are accessed using subscript indexing of their position. Slicing with colons can select ranges of elements

Uploaded by

kawibep229
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views20 pages

Chapter 2

This document discusses arrays in MATLAB. It covers: 1) Arrays store and manipulate data as lists of numbers arranged in rows and columns. One-dimensional arrays are lists in a single row or column called vectors. Two-dimensional arrays are lists arranged in multiple rows and columns called matrices. 2) Vectors are created using square brackets with numbers separated by spaces/commas. Matrices are created using square brackets with each row in semicolon-separated lists. Functions like zeros, ones, and eye can generate arrays of all zeros, ones, or identities. 3) Individual elements in arrays are accessed using subscript indexing of their position. Slicing with colons can select ranges of elements

Uploaded by

kawibep229
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arrays

Array
 The array is a fundamental form that MATLAB uses to store and manipulate data.
 An array is a list of numbers arranged in rows and/or columns.

ONE-DIMENSIONAL ARRAY (VECTOR)


 A one-dimensional array is a list of numbers arranged in a row or a column
Creating a vector
 The vector is created by typing the elements (numbers) inside square brackets [ ].
variable_name = [ type vector elements ]
Row vector:
To create a row vector type the elements with a space or a comma
between the elements inside the square brackets.
Column vector:
To create a column vector type the left square bracket [ and then enter the
elements with a semicolon between them, or press the Enter key after each
element. Type the right square bracket ] after the last element.
 Creating a vector with constant spacing by specifying the first term, the
spacing,and the last term:
variable_name = [m:q:n] or variable_name = m:q:n
Creating a vector with linear (equal) spacing by specifying the first and last
terms, and the number of terms:
How do we create an array containing all the numbers between 10 and 100 ?

>>x = 10 : 1 : 100

How can you create an array with numbers from 100 to 1 in decreasing order?

Concatenating 1D Arrays
x = 1 : 5;
y = 100 : 105;
z = [x, y, x] x, y and x are concatenated into one array

Z=
1 2 3 4 5 100 101 102 103 104 105 1 2 3 4 5
ARRAY ADDRESSING
Vector:The address of an element in a vector is its position in the row (or column).

x = 10 : -1 : 1;
y = x(3)
What are the outputs of the following commands?

x = 10 : -1 : 1; 2) x = 10 : -1 : 1;
1) y = x(end)
y = x([3,9,4])

Using Addressing to edit 1D arrays

x = 10 : -1 : 1;
x(2) = 10;
USING A COLON : IN ADDRESSING ARRAYS
 A colon can be used to address a range of elements in a vector or
a matrix.
 For a vector:
 va(:) Refers to all the elements of the vector va (either a row or a
column vector).
 va(m:n) Refers to elements m through n of the vector va.
 Example:
Adding elements to a vector:
We can erase(delete) elements of an vector

x = 10 : -1 : 1;
x(2) = []

x = 10 : -1 : 1;
x(2 : 4) = []
CREATING A TWO-DIMENSIONAL ARRAY (MATRIX)
variable_name = [1st row elements; 2nd row elements; 3rd
row elements; ... ; last row elements]
The zeros, ones and, eye Commands
zeros(m,n), ones(m,n), and eye(n)
THE TRANSPOSE OPERATOR( ’ )
The transpose operator, when applied to a vector,
When applied to a matrix, it switches the rows (columns) to
columns (rows).
2D Array Addressing:The address of an element in a matrix is its position,
defined by the row number and the column number where it is located.

ma(1,1) = 3 and ma(2,3) = 10.


USING A COLON : IN ADDRESSING ARRAYS

A(:,n) Refers to the elements in all the rows of


column n of the matrix A.
A(n,:) Refers to the elements in all the columns of
row n of the matrix A.
A(:,m:n) Refers to the elements in all the rows
between columns m and n of the matrix A.
A(m:n,:) Refers to the elements in all the columns
between rows m and n of thematrix A.
A(m:n,p:q) Refers to the elements in rows m through
n and columns p through q of the matrix A.
DELETING ELEMENTS
Adding elements to a matrix:

What does the matrix y look like?

y = [1 2 3; 4 5 6; 7 8 9];
y(2, :) = [];
y(: , [1 3]) = [];
y
BUILT-IN FUNCTIONS FOR HANDLING ARRAYS
Problems

1) Using the colon symbol, create a row vector (assign it to a variable named Fives) with
five elements that are all 5.?

2)
Answers

>>Fives(1:5)=5
>>Fives =
5 5 5 5 5

>>D=[zeros(3,4) [8:-1:6]']

>>D =
0 0 0 0 8
0 0 0 0 7
0 0 0 0 6

You might also like