logo

Selecting Specific Rows and Columns in a Matrix in MATLAB 📂Programing

Selecting Specific Rows and Columns in a Matrix in MATLAB

Methods

m×nm \times n Given a data in the form of a matrix, let’s call it AA. If you want to use only a specific part of matrix AA, you can use the following method.

B=A(a:b, c:d)

Running the code as above, BB becomes a (ba)×(dc)(b-a) \times (d-c) matrix containing the data from row aa to bb, column cc to dd of matrix AA. 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)

1.PNG

:: If you want to extract the entire rows or columns, you can use a colon. a3a3 extracts the entire columns, and a4a4 extracts the entire rows.

a3=A(3:7,:)

a4=A(:,4:9)

2.PNG

Using a colon is useful when extracting specific rows or columns.

a5=A(3,:)

a6=A(:,9)

5D91BC280.png