Create a matrix using the following code:
A = [1, 5, 2; 3, 6, 4];
A = 1 5 2 3 6 4
% Elements in 'all' rows but in the first column of A firstCol = A(:,1);
firstCol = 1 3
||Elements in the \(i\)-th row of
||Elements in the \(j\)-th column of
||Elements in the \(k\)-th plane of a 3D matrix
||All elements in
% Elements in the first row of A resultExample1 = A(1,:);
resultExample1 = 1 5 2
% Elements in the last two columns resultExample2 = A(:,2:3);
resultExample2 = 5 2 6 4
% All elements as a column vector resultExample3 = A(:);
resultExample3 = 1 3 5 6 2 4
I recommend the following books to learn more on the colon operator in MATLAB:
This book was the winner of a 2017 Textbook Excellence Award. The colon operator is explained clearly in the second chapter.
MATLAB for Engineers (5th Edition)
This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. The fourth chapter is completely dedicated to matrix manipulation.
I also recommend the following page in the MATLAB help documentation, and the other tutorials in this series, but especially this tutorial.
- MATLAB Concatenation Operator Explained
- How to Create Numeric Sequences Using the Colon Operator in MATLAB
- MATLAB Colon Operator in Matrix Indexing Expressions
- Multiline Statements in MATLAB
I hope you have learned how to use the colon operator for matrix indexing purposes. The source code developed in this tutorial is available at this page.