简体   繁体   English

如何将一个数组的多行和多列分配给 Python 中另一个数组的相同行和列?

[英]How can I assign multiple rows and columns of one array to the same rows and columns of another array in Python?

As the title says, how do I assign multiple rows and columns of one array to the same rows and columns of another array in Python?正如标题所说,如何将一个数组的多个行和列分配给 Python 中另一个数组的相同行和列?

I want to do the following:我想做以下事情:

 Kn[0, 0] = KeTrans[startPosRow, startPosCol];
 Kn[0, 1] = KeTrans[startPosRow, endPosCol];
 Kn[1, 0] = KeTrans[endPosRow, startPosCol];
 Kn[1, 1] = KeTrans[endPosRow, endPosCol];

Kn is a 2X2 matrix and KeTrans is a 4X4. Kn 是 2X2 矩阵,KeTrans 是 4X4。

I tried the following but with no luck.我尝试了以下但没有运气。

Kn[0:1, 0:1] = KeTrans[startPosRow: endPosRow, startPosCol: endPosCol]

For multi-dimensional arrays, I highly suggest use Numpy.对于多维 arrays,我强烈建议使用 Numpy。

import numpy as np

To create an Nth-dimensional array:创建第 N 维数组:

a = np.array([4,2,4],[23,4,3,2]...,[2,3,4])

The array are indexed very intuitively:数组的索引非常直观:

>> a[0,1]
4

You can even do slicing for the np array.您甚至可以对 np 数组进行切片。

documentation of numpy multi-dimensional array: https://numpy.org/doc/stable/reference/arrays.ndarray.html numpy 多维数组的文档: https://numpy.org/doc/stable/reference/arrays.ndarray.html

But they're not the same rows and columns:-) (unless startPosRow and friends have very specific values).但它们不是相同的行和列:-)(除非startPosRow和朋友有非常具体的值)。

The problem is that the slice startPosRow:endPosRow (for example) does not mean "element startPosRow and element endPosRow ".问题是切片startPosRow:endPosRow (例如)并不意味着“元素startPosRow和元素endPosRow ”。 It means "elements in range(startPosRow, endPosRow) ", which doesn't include endPosRow and which typically has more than two matching indices.它的意思是“ range(startPosRow, endPosRow) ”,它不包括endPosRow并且通常具有两个以上的匹配索引。

If you just want the four corners, you could use slices with a step size:如果你只想要四个角,你可以使用带步长的切片:

Kn[0:1, 0:1] = KeTrans[startPosRow:endPosRow + 1:endPosRow - startPosRow,
                       startPosCol:endPosCol + 1:endPosCol - startPosCol]

Is this what you are trying to do:这是你想要做的:

In [323]: X = np.arange(16).reshape(4,4)
In [324]: Y = np.zeros((2,2),int)
In [325]: Y[:] = X[:2,:2]
In [326]: Y
Out[326]: 
array([[0, 1],
       [4, 5]])
In [327]: Y[:] = X[1:3,2:]
In [328]: Y
Out[328]: 
array([[ 6,  7],
       [10, 11]])

For reference以供参考

In [329]: X
Out[329]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个大的 numpy 数组(5 列 300 万行)——如何选择同时满足多个条件的行? Python 3.8.8 - One large numpy array (3mil rows on 5 columns) - how to pick rows that meet several conditions at the same time? Python 3.8.8 如何在Python中将数组拆分为列和行? - How to split array into columns and rows in Python? PYTHON:如何使用三角形数组将行转换为列? - PYTHON: How do I transpose rows to columns with a triangular array? Numpy用另一个数组的特定行和列替换一个数组的特定行和列 - Numpy replace specific rows and columns of one array with specific rows and columns of another array 在python数组中移动行和列 - Shifting rows and columns in a python array 将值分配给具有相同行相同值的多行熊猫列 - Assign values to Multiple rows pandas columns with same rows same value 将多个 int 列/行合并为一个 numpy 数组(pandas 数据框) - Merge multiple int columns/rows into one numpy array (pandas dataframe) 将多列合并为具有相同行数的列 - Combine Multiple Columns into one with same amount of Rows 尝试在 python 中翻转数组中的列和行。 我可以打印它们,但不确定如何将它们变成一个新数组 - Trying to flip columns and rows in an array in python. I can print them, but not sure how make them into a new array Python - NumPy - 从数组中删除多个行和列 - Python - NumPy - deleting multiple rows and columns from an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM