MATLAB Answers

0

how to add a row and column to a matrix ?

Asked by polo Mahmoud on 24 Oct 2019
Latest activity Answered by Jos (10584)
on 24 Oct 2019
eg.
A = [1 2 3
4 5 6
7 8 9];
and transform it to;
A = [1 2 1 3
4 5 1 6
1 1 1 1
7 8 1 9];

  0 Comments

Sign in to comment.

Products


Release

R2019a

1 Answer

Answer by Jos (10584)
on 24 Oct 2019
 Accepted Answer

One easy option is to do this for rows and columns separately
A = [1 2 3 ; 4 5 6 ; 7 8 9]
x = 3 ; % add a row/column of ones before this row/column
A(end+1, :) = 1 % add row add the end
A([x end], :) = A([end x], :) % swap the x-th and last row
% do the same for columns
A(:, end+1) = 1
A(:, [x end]) = A(:, [end x])

  0 Comments

Sign in to comment.