简体   繁体   English

给定大小为3 x 3的矩阵垫,请找到其最终累积总和大于或等于tO 150的每一行中的所有偶数

[英]Given a matrix mat of size 3 x 3. Find all the even numbers situated in each of the row(s) whose end cumulative sum is greater than or equal tO 150

PROBLEM: Given a matrix mat of size 3 x 3. Find all the even numbers situated in each of the row(s) whose end cumulative sum is greater than or equal to 150. 问题:给定大小为3 x 3的矩阵垫,请找出位于其最终累积总和大于或等于150的每一行中的所有偶数。

I am able to find sum but not able to extract row wise sum and also I have issues in assigning these row wise even number sum to a variable. 我能够找到和,但无法提取按行和,并且在将这些按行偶数和分配给变量时也遇到问题。

So My code looked but then I don't know how to extract the row wise sum of even numbers. 因此,我的代码看起来不错,但是后来我不知道如何提取偶数的行明智和。

import numpy as np 
mat = np.array([[51,21,14], 
                [56,85,22], 
                [99,666,230]]).reshape(3,3)
mat2=mat[mat%2==0]
res_mat=np.cumsum(mat2[:1])
print(mat2)
print(res_mat)

this answer won't be any good if using numpy is a requirement for your problem, not using it might be easier though. 如果使用numpy解决您的问题,此答案将不会有什么好处,但是不使用它可能会更容易。

mat = [[51, 21, 14], [56, 85, 22], [99, 666, 230]]
even_numbers_in_target_rows = list()
for row in mat:
    if sum(row) >= 150:
        for val in row:
            if val % 2 == 0:
                even_numbers_in_target_rows.append(val)

print(even_numbers_in_target_rows)

Steps: 脚步:

  1. Loop through matrix row by row 逐行循环遍历矩阵
  2. Sum row. 总和行。
  3. Check if row sum is over or equal to 150 检查行总和是否大于或等于150
  4. If over, then step through each value 如果结束,则逐步遍历每个值
  5. If even add to list of even numbers in rows over or equal to 150 如果将偶数加到大于或等于150的行中的偶数列表中

If I understand the problem correctly (and I feel that it is written in an awkward way), you have to find the rows whose sums are >= 150: 如果我正确理解了该问题(并且感觉写起来很尴尬),则必须找到总和> = 150的行:

rows = mat[mat.sum(axis=1) >= 150]

And then choose the even numbers from them: 然后从中选择偶数:

rows[rows % 2 == 0]
#array([ 56,  22, 666, 230])

Note: the question is a bit ambiguous because it is not clear whether "whose" refers to the rows or to the even numbers within. 注意:这个问题有点模棱两可,因为不清楚“谁”是指行还是偶数。 I'm assuming the latter. 我假设是后者。 The former would be easier. 前者会更容易。

You can use np.bincount together with np.where 您可以将np.bincountnp.where一起使用

# find even numbers 
even = mat&1 == 0
# translate mask into coordinates
y, x = np.where(even)
# use y coordinate to group into rows
np.bincount(y, mat[even], mat.shape[0])

# array([ 14.,  78., 896.])

You can do this by taking the sum along each row and using the resultant boolean array to index into the original. 您可以通过沿每一行取和,然后使用所得的布尔数组将其索引到原始数组中来进行此操作。 Then, just get the even numbers: 然后,只需获得偶数:

mat = np.array([[51, 21, 14], 
                [56, 85, 22], 
                [99, 666, 230]])

uniques = np.unique(mat[mat.sum(axis=1) > 150])

uniques[uniques % 2 == 0]

Output: 输出:

array([ 22,  56, 230, 666])

暂无
暂无

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

相关问题 从4个给定的数组(未排序)中,找到每个数组的元素,其总和等于某个数字X. - From 4 given arrays(not sorted), find the elements from each array whose sum is equal to some number X 在python 2.7中查找矩阵中每一列等于或大于1时的行号 - Find row number when each column in a matrix is equal to or greater than 1 in python 2.7 对于数字列表,查找累积和保持在范围内的所有组合 - For a list of numbers, find all combinations whose cumulative sum stays within bounds 在pandas中,如何找到累积和大于阈值的行/索引? - In pandas, how to find the row/index where the cumulative sum is greater than a threshold? 总和小于给定总和的最大子集 - Largest Subset whose sum is less than equal to a given sum 给定一个数字列表,找到所有矩阵,使得每列和每行的总和为 264 - Given a list of numbers, find all matrices such that each column and row sum up to 264 查找一行中每个分组的累积总和,然后将分组设置为等于最大总和 - Find cumulative sums of each grouping in a row and then set the grouping equal to the maximum sum 编写 python 代码以找到三个数字 x、y 和 z。 用户给出了谁的总和和乘积? - write a python code to find the three numbers x , y and z . whose sum and product is given by the user? 查找其第一个元素是不大于给定数字的最大值的子列表 - find the sublist whose first element is the maximum that is no greater than a given number 如何找到它们的总和等于给定数字的数字? - How to find the numbers that their sum equal to a given number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM