OKPEDIA MATLAB EN MATRICE

How to extract two or more columns of a matrix in Matlab and Octave

To extract two or more columns from a matrix on Matlab and Octave type

M(:,a:b)

At the first parameter insert the symbol: to take all the rows of the matrix

At the second parameter after the comma, insert the a:b range of the columns

  • term a is the number of the first column to extract
  • term a is the number of the last column to extract

Alternative method

Digitare nel secondo parametro la lista delle colonne da estrarre tra parentesi quadre, separandole tra loro da uno spazio o da una virgola.

M(:,[a b])

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

Example

Example 1

Define a 2x3 matrix with two rows and three columns

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

Extract the first two columns of the matrix M

Type M(:,1:2)

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

This command extracts the first and second columns.

Example 2

Extract the first and third columns of the matrix.

Digitare M(:,[1 3])

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

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

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


Report an error or share a suggestion to enhance this page


Matrix in Matlab/Octave


FacebookTwitterLinkedinLinkedin