numpy-在特定位置重叠2个矩阵 [英] Numpy - Overlap 2 matrices at a particular position
问题描述
美好的一天,
让我们说我有一个200 * 200的矩阵A和一个50 * 50的矩阵B.我想将B放置在点A上,例如(175,175).问题是B超出了A的范围,但我真正需要的是将超出的范围剪掉.
Lets say I have a 200*200 matrix A, and 50*50 matrix B. I want B to be placed on the point A at say (175,175). The problem with this is that B exceeds the bounds of A, but what I really need is for the exceeded bounds to clip off.
我尝试做这样的事情:A [y:yRange + y,x:xRange + x] = B,但这会引发超出范围的错误.有什么简单的方法可以将这2个代码重叠在一个位置上,而不必求助于慢的for循环.
I tried doing something like this: A[y:yRange+y, x:xRange+x] = B, but this will throw the out of bounds error. Is there any easy way to overlap these 2 at a position without resorting to a slow for loop.
推荐答案
您可以将B
的一个片段复制到A
的一个片段上,例如:
You can copy a slice of B
onto a slice of A
, for example:
A[175:, 175:] = B[:25, :25]
这是对numpy
索引的直接使用.
It's a straight forward use of numpy
indexing.
这篇关于numpy-在特定位置重叠2个矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!