Group Abstract Group Abstract

Message Boards Message Boards

1
|
3.7K 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
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
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