OKPEDIA MATLAB EN MATRICE

How to replace a column of the matrix in Matlab and Octave

To change the values of a single column of a matrix in Matlab / Octave type

M(:,n)=[ k ]

  • M is the name of the array
  • n is the number of the column to be replaced
  • k is the list of new column values

Example

Define a 3x2 matrix

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

To replace the first column of the matrix type M(:,1)=[ 7; 8; 9]

>> M(:,1)=[ 7; 8; 9]

This command replaces all values in the first column. The other values of the matrix are unchanged.

M =
7 2
8 4
9 6

To replace the second column of the matrix type M(:,2)=[ 3; 5; 7]

>> M(:,2)=[ 3; 5; 7]

This command changes the values of the second column of the matrix

M =
7 3
8 5
9 7

https://how.okpedia.org/en/matlab/how-to-replace-a-column-of-the-matrix-in-matlab-and-octave


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


Matrix in Matlab/Octave


FacebookTwitterLinkedinLinkedin