How to add rows and colums of zero?

2 ビュー (過去 30 日間)
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2019 年 1 月 3 日
編集済み: madhan ravi 2019 年 1 月 3 日
I want to compare two matrices for that the number of rows and columns should be similar
The two matrices to be compared are as follows
A=[3 4 5; 3 4 5; 3 4 5]
B=[1 2 3 4 5 ; 1 2 3 4 5; 1 2 3 4 5]
Consider the numbers also as their position in the matrix for ease of understanding
That means we need two rows and two colums of zero at the start
the expected matrix of is as follows
A=[0 0 3 4 5;0 0 3 4 5;0 0 3 4 5]
P.S. The number of colums and rows of zero are not always the same. that could be varying like 3 colums and 1 rows and further

採用された回答

madhan ravi
madhan ravi 2019 年 1 月 3 日
編集済み: madhan ravi 2019 年 1 月 3 日
A = padarray(A,[abs(size(B,1)-size(A,1)) abs(size(B,2)-size(A,2))],0,'pre') % requires image processing toolbox
Gives:
A =
0 0 3 4 5
0 0 3 4 5
0 0 3 4 5
  3 件のコメント
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2019 年 1 月 3 日
But i dont know about the rows or a code with wich i can add both rows and colums of zero
madhan ravi
madhan ravi 2019 年 1 月 3 日
編集済み: madhan ravi 2019 年 1 月 3 日
Did you try my answer?
n=2; % no of zeros(rows & columns) to be padded
A=[zeros(n,size(B,2));zeros(size(A,1),n) A]
Gives:
A =
0 0 0 0 0
0 0 0 0 0
0 0 3 4 5
0 0 3 4 5
0 0 3 4 5

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by