0% found this document useful (0 votes)
134 views7 pages

1 - Matrix Indexing - MATLAB & Simulink - MathWorks India

This document discusses matrix indexing in MATLAB. It explains that matrices can be indexed using subscript notation with row and column numbers (e.g. A(2,3)) or linear indexing with a single subscript (e.g. A(6)). It also covers more advanced indexing with logical arrays, colons, and multi-dimensional arrays.

Uploaded by

pand2406
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)
134 views7 pages

1 - Matrix Indexing - MATLAB & Simulink - MathWorks India

This document discusses matrix indexing in MATLAB. It explains that matrices can be indexed using subscript notation with row and column numbers (e.g. A(2,3)) or linear indexing with a single subscript (e.g. A(6)). It also covers more advanced indexing with logical arrays, colons, and multi-dimensional arrays.

Uploaded by

pand2406
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
You are on page 1/ 7

Matrix Indexing - MATLAB & Simulink - MathWorks India

CONTENTS

http://in.mathworks.com/help/matlab/math/matrix-indexing.html

Close

matrix, specify its row and column number using the following syntax, where A is the matrix
nd column second:

, column 2 with

ons, specify additional indices following the row and column indices. See the section on

matrix with a single subscript, A(k). MATLAB stores matrices and arrays not in the
d in the MATLAB Command Window, but as a single column of elements. This single
ns from the matrix, each appended to the last.

(value = 5) can also be identified as element 6 in the actual storage sequence. To


e of using the standard A(3,2) syntax, or you can use A(6), which is referred to as linear

B calculates an index into the storage column based on the dimensions you assigned to the
ensional array like A has size [d1 d2], where d1 is the number of rows in the array and d2
y two subscripts (i, j) representing row-column indices, the offset is

1 of 7

4/1/2015 6:39 PM

Matrix Indexing - MATLAB & Simulink - MathWorks India

http://in.mathworks.com/help/matlab/math/matrix-indexing.html

B calculates the offset into A's storage column as (2-1) * 3 + 3, or 6. Counting down six

want to use linear indexing instead, you can convert to the latter using the sub2ind
the previous section, sub2ind changes a standard row-column index of (3,2) to a linear

A), 3, 2)

inear index, use the ind2sub function:

is possible to compute the sum of the elements in the fourth column of A by typing

sion using the colon operator. Subscript expressions involving colons refer to portions of a

of column n of matrix A. Using this notation, you can compute the sum of the fourth

a matrix, use the colon operator with a step value. The m:3:n in this expression means to
ment in the matrix. Note that this example uses linear indexing:

2 of 7

4/1/2015 6:39 PM

Matrix Indexing - MATLAB & Simulink - MathWorks India

http://in.mathworks.com/help/matlab/math/matrix-indexing.html

xing that uses one array as the index into another array. You can base this type of indexing
elements in the indexing array.

exing where array B indexes into elements 1, 3, 6, 7, and 10 of array A. In this case, the
e intended elements of A:

25

30

35

40

45

50

vector, the orientation of the indexed vector is honored for the output:

tor, the shape of the indices is honored:

designate the last element in a particular dimension of an array. This keyword can be useful
not know how many rows or columns there are in a matrix. You can replace the expression

meanings in MATLAB. It can be used as explained above, or to terminate a conditional


cks, or to terminate a nested function.

ments in a row or column of a matrix. Using the following syntax, you can compute the sum
f a 4-by-4 magic square A:

3 of 7

4/1/2015 6:39 PM

Matrix Indexing - MATLAB & Simulink - MathWorks India

http://in.mathworks.com/help/matlab/math/matrix-indexing.html

you can refer to all elements in the entire matrix. This example displays all the elements of

ements of an array A based on their position in the indexing array, B, not their value. In this
element in the indexing array is treated as a positional index into the array being accessed.

of logical ones and zeros. The position of these elements in B determines which elements

ogical arrays as it returns the linear indices of nonzero elements in B, and thus helps to

4 of 7

4/1/2015 6:39 PM

Matrix Indexing - MATLAB & Simulink - MathWorks India

http://in.mathworks.com/help/matlab/math/matrix-indexing.html

at satisfies the condition A > 0.5, and uses the positions of ones in B to index into A:

et the random number generator

576
0
0
854
0

0.1419
0.4218
0
0
0

0
0.0357
0
0
0

on of the prime numbers in a magic square using logical indexing to set the nonprimes to 0:

5 of 7

% Logical indexing

4/1/2015 6:39 PM

Matrix Indexing - MATLAB & Simulink - MathWorks India

http://in.mathworks.com/help/matlab/math/matrix-indexing.html

ay should have the same number of elements as the array being indexed into, but this is not
have smaller (but not larger) dimensions:

f the indexing array as if they were present and set to zero, as in array C below:

ay C to give it the same number of

ent Array Types

AB array using a single colon, MATLAB returns a column vector (see variable n, below).
array using a single colon, you get a comma-separated list (see Access Data in a Structure
for more information.)

'}, 1);

6 of 7

s(:,2)=s(:,1);

4/1/2015 6:39 PM

Matrix Indexing - MATLAB & Simulink - MathWorks India

http://in.mathworks.com/help/matlab/math/matrix-indexing.html

s(:).a
ans =
1
ans =
2
ans =
1
ans =
2

x to another matrix, you can use any of the styles of indexing covered in this section. Matrix
ollowing requirement.
, subscripts J, K, M, N, etc. may be scalar, vector, or array, provided that all of the

, not including trailing subscripts equal to 1, does not exceed ndims(B).

s specified for A equals the number of nonscalar subscripts specified for B. For example,
lid because both sides of the equation use one nonscalar subscript.

ar subscripts specified for A matches the order and length of nonscalar subscripts specified
9) = B(5:8, 1:7) is valid because both sides of the equation (ignoring the one scalar
ript followed by a 7-element subscript.

7 of 7

4/1/2015 6:39 PM

You might also like