有没有办法我只能从矩阵中的每一行和每列中获取所需的索引元素范围?

问题描述 投票:0回答:2

我们假设我有一个10x10元素的矩阵。我想要的是从每行到第6行从第0列到第7列的元素,并丢弃所有其他元素。例如:

[08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 
49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 
81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 
52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 
22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 
24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 
32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 
67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 
24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 
21, 36, 23, 09, 75, 00, 76, 44, 20, 45, 
78, 17, 53, 28, 22 75, 31, 67, 15, 94]  

我只需要从0到7列的元素,从每行到第6行。你可以看到图像。 This selected portion of the matrix is what I need.

python
2个回答
1
投票

由于您的矩阵存储在一维列表中,我假设您有一个任意变量(actual_cols),它将确定一行可以拥有的列数。我还假设您希望提取的矩阵将存储在类似于原始矩阵的1维列表中。如果是这些情况,那么你可以尝试这种方法:

a = [8, 2, 22, 97, 38, 15, 00, 40, 0, 75, 
49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 
81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 
52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 
22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 
24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 
32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 
67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 
24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 
21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 
78, 17, 53, 28, 22, 75, 31, 67, 15, 94]

actual_cols = 10
col = 7
row = 6

b = [a[i] for i in range(len(a)) if (i % actual_cols < col) and (i / actual_cols) < row]
print(b)

如果要将提取的矩阵存储在二维列表中,也可以执行类似的操作

a = [8, 2, 22, 97, 38, 15, 00, 40, 0, 75, 
49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 
81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 
52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 
22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 
24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 
32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 
67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 
24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 
21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 
78, 17, 53, 28, 22, 75, 31, 67, 15, 94]

actual_cols = 10
col = 7
row = 6

b = [[a[i*actual_cols + j] for j in range(col)] for i in range(int(len(a)/actual_cols)) if i < row]
print(b)

如果将原始矩阵存储在二维列表中,则更容易提取它们

a = [[8, 2, 22, 97, 38, 15, 00, 40, 0, 75],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87], 
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71], 
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24], 
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92], 
[24, 47, 32, 60, 99, 3, 45, 2, 44, 75], 
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38], 
[67, 26, 20, 68, 2, 62, 12, 20, 95, 63], 
[24, 55, 58, 5, 66, 73, 99, 26, 97, 17], 
[21, 36, 23, 9, 75, 0, 76, 44, 20, 45], 
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94]]

col = 7
row = 6

b = [x[:col] for x in a[:row]]
print(b)

0
投票

我的方法是将矩阵转换为一维数组并根据它们的位置选择数字。此外,这种方法可以保持您的数字格式,如“02”,因为我基本上使用字符串和列表方法。

现在我假设您已将矩阵转换为一维数组。由于您的需求数字分为0-7列和0-6行,因此您基本上只需要8/10列和6/11行。这意味着你只想迭代前6 * 10 = 60个数字(10是一行的len),我们的目标是0-7,10-17,20-27的位置......

检查下面的代码3。

new_l = [] # this is to store all qualified numbers (falling in the requested columns and requested rows)
position_list = list(range(6*10)) # you only want to iterate the first 6 rows, so 60 numbers in total
for i in range(0,60,10):
    line_position_list =position_list[i:i+8] # slice the list into 6 smaller pieces and extract only the first 8 elements.
    for position in line_position_list:
        new_l.append(l[position]) # append these numbers into the new list
print(new_l)

# if you want to keep the shape of array, turn everything to string and then add \n at the end of the 7th element
for i in range(0,len(new_l),8):
    new_l[i] = "\n" + new_l[i]
print(new_l)
new_string = ",".join(new_l)
print(new_string)

Output

08, 02, 22, 97, 38, 15, 00, 40,
 49, 49, 99, 40, 17, 81, 18, 57,
 81, 49, 31, 73, 55, 79, 14, 29,
 52, 70, 95, 23, 04, 60, 11, 42,
 22, 31, 16, 71, 51, 67, 63, 89,
 24, 47, 32, 60, 99, 03, 45, 02

更新:l是所有字符串类型元素的1D数组:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.