OKPEDIA MATLAB EN MATRICE

How to extract two or more rows from a matrix in Matlab and Octave

To extract two or more rows of a matrix on Matlab and Octave

M(a:b,:)

In the first parameter indicate the interval a:b between the rows

  • term a is the number of the first row to be extracted
  • term b is the number of the last row to be extracted

In the second parameter after the comma indicate the symbol : to take all the columns of the matrix

Alternative method

Indicate the list of rows to be extracted in square brackets at the first parameter.

Separate the lines from each other by a space or a comma.

M([a b],:)

This method allows you to extract even the rows of the matrix that are not close to each other.

Example

Example 1

Define a 3x2 matrix with three rows and two columns

>> M=[1 2; 3 4 ; 5 6]
M =
1 2
3 4
5 6

Extract the first two rows of matrix M

>> M(1:2,:)
ans =
1 2
3 4

The command extracts the first and second rows.

Example 2

Extract the first and third rows of the matrix.

>> M([1 3],:)
ans =
1 2
5 6

The command extracts the first and third rows of the M matrix.

https://how.okpedia.org/en/matlab/how-to-extract-two-or-more-rows-from-a-matrix-in-matlab-and-octave


Report us an error or send a suggestion to improve this page


Matrix in Matlab/Octave


FacebookTwitterLinkedinLinkedin