Increase matrix size, with the first matrix a the center of the new matrix

1 回表示 (過去 30 日間)
Alberto Paniate
Alberto Paniate 2020 年 10 月 12 日
コメント済み: Turlough Hughes 2020 年 10 月 12 日
Hi,
suppose to have a matrix (2000x2000) now i want to copy it in a bigger matrix (4000x4000 for example), with the original matrix at the center of the second matrix, and the other values set to 0. How can I do?
I have done it with for loop, but it is slow

回答 (1 件)

Turlough Hughes
Turlough Hughes 2020 年 10 月 12 日
編集済み: Turlough Hughes 2020 年 10 月 12 日
There is a function made specifically for this called padarray, try the following example:
A = rand(2000);
B = padarray(A,[1000 1000]);
  2 件のコメント
Alberto Paniate
Alberto Paniate 2020 年 10 月 12 日
So if I have my matrix, A 2000x2000 and I create B=zeros(4000,4000) an I want to put at the center of B, I have to write C= padarray(A, [1000 100])?
Turlough Hughes
Turlough Hughes 2020 年 10 月 12 日
Creating an array B=zeros(4000) is unnecessary because you are not using it in the next step. You can simply write:
C = padarray(A,[1000 1000]);
to get what you are looking for.

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

カテゴリ

Help Center および File ExchangeFilter Banks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by