Group Abstract Group Abstract

Message Boards Message Boards

1
|
3.4K Views
|
2 Replies
|
1 Total Like
View groups...
Share
Share this post:

Creating a function to insert a small matrix to a big one?

Posted 9 years ago

Ok so the question is i need to write a function "copymatrix[]" such that if A and B are matrices then copymatrix[A,B,col,row] makes a copy of B inside A by replacing A[[ i + col, j+row]] by B[[i,j]]. Assume that B fits inside A.. Im just not sure on how to define the matrices [A,B]. Thanks

POSTED BY: Chris Maniel
2 Replies

Something like this works:

ClearAll[CopyMatrix]
CopyMatrix[a_, b_, col_Integer, row_Integer] := Module[{c = a, dimx, dimy},
  {dimx, dimy} = Dimensions[b];
  c[[row ;; row + dimx - 1, col ;; col + dimy - 1]] = b;
  c
]

a = RandomReal[1, {10, 10}];
b = {{1, 1, 0}, {0, 2, 2}};    
a // MatrixForm
b // MatrixForm
CopyMatrix[a, b, 3, 5] // MatrixForm

Note that there is no error checking in place yet: so make sure col and row are not too big or negative, and matrix b is smaller than matrix a...

POSTED BY: Sander Huisman
Posted 9 years ago

What are the dimx,dim y? Is that the values or size of the matrix?

POSTED BY: Chris Maniel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard