Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想比较两个numpy数组并创建第三个数组_Python_Arrays_Numpy_Simpleitk - Fatal编程技术网

Python 我想比较两个numpy数组并创建第三个数组

Python 我想比较两个numpy数组并创建第三个数组,python,arrays,numpy,simpleitk,Python,Arrays,Numpy,Simpleitk,正如标题所说,我想比较两个以1和0为元素的sitk数组,并创建一个第三个数组,其中两个数组都以1和0表示任何其他情况。数组大小相同,并且是三维的,但是有没有比使用嵌套for循环遍历数组更有效的方法呢 import numpy as np a = np.random.randint(low=0, high=2, size=(2,3,4), dtype=np.int) print(a) b = np.random.randint(low=0, high=2, size=(2,3,4), dtyp

正如标题所说,我想比较两个以1和0为元素的sitk数组,并创建一个第三个数组,其中两个数组都以1和0表示任何其他情况。数组大小相同,并且是三维的,但是有没有比使用嵌套for循环遍历数组更有效的方法呢

import numpy as np

a = np.random.randint(low=0, high=2, size=(2,3,4), dtype=np.int)
print(a)

b = np.random.randint(low=0, high=2, size=(2,3,4), dtype=np.int)
print(b)

c = np.logical_and(a,b).astype(int)
print(c)
这就是你要找的吗

arr_shape = (1,4,3)
a = np.random.randint(low=0,high=2, size=arr_shape)
print(a)
b = np.random.randint(low=0,high=2, size=arr_shape)
print(b)
# the new array. subtract a and b and get the absolute value.
# then invert to get the required array
d = (~abs(b - a).astype(bool)).astype(int)
print(d)
这就是你要找的吗

arr_shape = (1,4,3)
a = np.random.randint(low=0,high=2, size=arr_shape)
print(a)
b = np.random.randint(low=0,high=2, size=arr_shape)
print(b)
# the new array. subtract a and b and get the absolute value.
# then invert to get the required array
d = (~abs(b - a).astype(bool)).astype(int)
print(d)
输出:

[[[1 1 0]
  [1 0 0]
  [0 1 1]
  [1 1 0]]]
[[[0 1 0]
  [0 1 0]
  [1 0 0]
  [0 0 1]]]
array([[[0, 1, 1],
        [0, 0, 1],
        [0, 0, 0],
        [0, 0, 0]]])
输出:

[[[1 1 0]
  [1 0 0]
  [0 1 1]
  [1 1 0]]]
[[[0 1 0]
  [0 1 0]
  [1 0 0]
  [0 0 1]]]
array([[[0, 1, 1],
        [0, 0, 1],
        [0, 0, 0],
        [0, 0, 0]]])

如果您有SimpleTk图像,则可以使用And函数

import SimpleITK as sitk
result = sitk.And(image1, image2)
这是AndImageFilter的功能版本。您可以在此处阅读该类的文档:

如果您有SimpleTk图像,可以使用And函数

import SimpleITK as sitk
result = sitk.And(image1, image2)
这是AndImageFilter的功能版本。您可以在此处阅读该类的文档:

这比我想象的要优雅得多。谢谢很高兴我能帮上忙,请把问题标记为当时的答案。这比我想的要优雅得多。谢谢很高兴我能帮上忙,请把问题标记为当时的答案。