Put a small matrix in a bigger one.

69 ビュー (過去 30 日間)
Ayob
Ayob 2013 年 6 月 2 日
コメント済み: Preet Lal 2022 年 5 月 20 日
I want to put a small matrix (p*q) called B into a bigger matrix (m*n) called A. How can I do it. Matrix B should be put on the left-right corner of matrix A.
for better understanding take a look on this image:

採用された回答

chibole
chibole 2016 年 10 月 19 日
function A = MatrixReplace(A,B)
[p,q] = size(B);
A(end-p+1:end, end-q+1:end) = B;
end
  3 件のコメント
Image Analyst
Image Analyst 2017 年 3 月 29 日
Be careful with this code. For one thing, it puts the pasted matrix in the lower right corner, not at some arbitrary row and column (row "i" and column "j") like the poster asked for. It also has no error checking, has no comments, and doesn't use descriptive variable names. Personally, I would not use this code in my company. For more flexible and robust way, see my answer.
chibole
chibole 2017 年 7 月 22 日
"For one thing, it puts the pasted matrix in the lower right corner" That is what is asked in the question.

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2013 年 6 月 2 日
編集済み: Image Analyst 2013 年 6 月 2 日
Try this fairly robust, more general code where you can specify the upper left corner and it will check to make sure the small array does not go outside the big array.:
% Create sample data:
big = ones(10)
small = 9 * ones(3)
% Get sizes
[rowsBig, columnsBig] = size(big);
[rowsSmall, columnsSmall] = size(small);
% Specify upper left row, column of where
% we'd like to paste the small matrix.
row1 = 5;
column1 = 3;
% Determine lower right location.
row2 = row1 + rowsSmall - 1
column2 = column1 + columnsSmall - 1
% See if it will fit.
if row2 <= rowsBig
% It will fit, so paste it.
big(row1:row2, column1:column2) = small
else
% It won't fit
warningMessage = sprintf('That will not fit.\nThe lower right coordinate would be at row %d, column %d.',...
row2, column2);
uiwait(warndlg(warningMessage));
end
  3 件のコメント
Image Analyst
Image Analyst 2022 年 5 月 20 日
If you know the size of the matrices to be pasted onto the underlying matrix, and the locations, then you can figure out the overlap region. Then just use normal indexing to get the mean. For example if you're pasting a 2x3 into the upper left corner of a 5x5 matrix, and a 2x3 onto the upper right corner, then the last column of the first matrix you paste will overlap the left column of the second 2x3 matrix, and they are both in column 3 of the 5x5 matrix.
Preet Lal
Preet Lal 2022 年 5 月 20 日
I am not clear,
If possible could you please share a short demo code.

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


Andrei Bobrov
Andrei Bobrov 2013 年 6 月 2 日
編集済み: Andrei Bobrov 2013 年 6 月 2 日
Use function padarray from Image Processing Toolbox
A(padarray(true(size(B)),size(A)-size(B),'pre')) = B;
or
A(blkdiag(false(size(A)-size(B)),true(size(B)))) = B;

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by