Selecting Specific Rows and Columns in a Matrix in MATLAB
Methods
$m \times n$ Given a data in the form of a matrix, let’s call it $A$. If you want to use only a specific part of matrix $A$, you can use the following method.
B=A(a:b, c:d)
Running the code as above, $B$ becomes a $(b-a) \times (d-c)$ matrix containing the data from row $a$ to $b$, column $c$ to $d$ of matrix $A$. Below is the example code and its result.
for k=1:9
for l=1:9
A(k,l)=10*k+l;
end
end
A
a1=A(3:7,4:9)
a2=A(2:5,1:6)
:
: If you want to extract the entire rows or columns, you can use a colon. $a3$ extracts the entire columns, and $a4$ extracts the entire rows.
a3=A(3:7,:)
a4=A(:,4:9)
Using a colon is useful when extracting specific rows or columns.
a5=A(3,:)
a6=A(:,9)