how to add a row and column to a matrix ?

300 ビュー (過去 30 日間)
polo Mahmoud
polo Mahmoud 2019 年 10 月 24 日
コメント済み: abdalaziz alkassm 2020 年 7 月 16 日
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];

採用された回答

Jos (10584)
Jos (10584) 2019 年 10 月 24 日
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])
  1 件のコメント
abdalaziz alkassm
abdalaziz alkassm 2020 年 7 月 16 日
Thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by