根据2D numpy数组过滤3D numpy数组 [英] filtering a 3D numpy array according to 2D numpy array

查看:117
本文介绍了根据2D numpy数组过滤3D numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形状为(3024,4032)的2D numpy数组。

I have a 2D numpy array with the shape (3024, 4032).

我有一个形状为(3024,4032,3)的3D numpy数组。

I have a 3D numpy array with the shape (3024, 4032, 3).

2D numpy数组填充为0和1。

2D numpy array is filled with 0s and 1s.

3D numpy数组填充了0到255之间的值。

3D numpy array is filled with values between 0 and 255.

通过查看2D数组的值,我想更改3D数组中的值。如果2D数组中的值为0,我将沿最后一个轴将3D数组中的所有3个像素值更改为0。如果2D数组中的值为1,则不会更改。

By looking at the 2D array values, I want to change the values in 3D array. If a value in 2D array is 0, I will change the all 3 pixel values in 3D array into 0 along the last axes. If a value in 2D array is 1, I won't change it.

我已检查了此问题,如何使用另一个数组的值过滤numpy数组,但适用于2个尺寸相同的数组。在我的情况下,维度是不同的。

I have checked this question, How to filter a numpy array with another array's values, but it applies for 2 arrays which have same dimensions. In my case, dimensions are different.

如何在两个数组中应用过滤,两个数组的大小相同,而最后一个维度的大小不一样?

How the filtering is applied in two arrays, with same size on 2 dimensions, but not size on the last dimension?

推荐答案

好吧,我将回答这个问题以强调有关缺失尺寸的一种说法。假设 a.shape ==(5,4,3) b.shape ==(5,4)

Ok, I'll answer this to highlight one pecularity regarding "missing" dimensions. Lets' assume a.shape==(5,4,3) and b.shape==(5,4)

索引时,现有维度对齐,这就是@Divakar解决方案 a [b == 0] = 0 有效。

When indexing, existing dimensions are left aligned which is why @Divakar's solution a[b == 0] = 0 works.

广播时,现有尺寸为正确对齐,这就是@InvaderZim的 a * b 无法正常工作的原因。您需要做的是 a * b [...,无] ,它会在右侧插入一个可广播的维度

When broadcasting, existing dimensions are right aligned which is why @InvaderZim's a*b does not work. What you need to do is a*b[..., None] which inserts a broadcastable dimension at the right

这篇关于根据2D numpy数组过滤3D numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆