NumPy总和沿不相交索引(NumPy sum along disjoint indices)
我有一个应用程序,我需要在3D NumPy数组中对任意索引组进行求和。 内置的NumPy数组求和例程对一个ndarray维度的所有索引进行求和。 相反,我需要总结我的数组中的一个维度的索引范围,并返回一个新的数组。
例如,让我们假设我有一个形状的ndarray
(70,25,3)
。 我希望总结沿着特定索引范围的第一个维度并返回一个新的3D数组。 考虑从0:25, 25:50
和50:75
的总和0:25, 25:50
这将返回一个形状数组(3,25,3)
。是否有一种简单的方法可以在NumPy数组的一维上执行“不相交的和”来产生这个结果?
I have an application where I need to sum across arbitrary groups of indices in a 3D NumPy array. The built-in NumPy array sum routine sums up all indices along one of the dimensions of an ndarray. Instead, I need to sum up ranges of indices along one of the dimensions in my array and return a new array.
For example, let's assume that I have an ndarray with shape
(70,25,3)
. I wish to sum up the first dimension along certain index ranges and return a new 3D array. Consider the sum from0:25, 25:50
and50:75
which would return an array of shape(3,25,3)
.Is there an easy way to do "disjoint sums" along one dimension of a NumPy array to produce this result?
原文:https://stackoverflow.com/questions/32798519
满意答案
有了两个整数,你只需使用位移和
|
:high = 0x00000000fe000000 low = 0x000000000001009f result = (high << 32) | low
对于任何其他输入,我建议先将它们转换为
int
,然后将结果转换回您想要的任何形式。要检索原始片段,请反向使用位移
high = result >> 32
和
&
与一个适当的面具:low = result & 0xffffffff
With two ints, you simply use bit-shifting and
|
:high = 0x00000000fe000000 low = 0x000000000001009f result = (high << 32) | low
For any other input, I suggest converting them to
int
s first, and converting the result back to whatever form you want.To retrieve the original pieces, use bit-shifting in reverse
high = result >> 32
and
&
with an appropriate mask:low = result & 0xffffffff
相关问答
更多N位的二进制组合,最多M“1”(Binary combinations of N bits with at most M “1”)
在python中将字节操作到二进制级别(manipulate bytes down to binary levels in python)
在Python中将整数转换为二进制,并比较这些位(Convert integer to binary in python and compare the bits)
Python中二进制的浮点表示(位不是十六进制)(Float representation of binary in Python (bits not hex))
Python二进制操作 - 组合高位和低位(Python binary operations - combine higher and lower order bits)
Python:选择代表二进制数的位数(Python: Choose number of bits to represent binary number)
如何组合char矢量的二进制第n位(how to combine binary nth bit of char vector)
Python:将一个字节转换为二进制并移位它的位?(Python: convert a byte to binary and shift its bits?)
LAHF(用标志寄存器的低位字节加载AH)(LAHF (load AH with lower order byte of the flag register))
如何在python中使用XOR进行二进制操作?(How do binary operations with XOR in python work?)
相关文章
更多求 java生产一个不相同的编号
求一个group by后面字段顺序影响结果的例子
SQL中GROUP BY与HAVING的用法
elasticsearch 口水篇(6) Mapping 定义索引
elasticsearch RESTful搜索引擎-(java jest 使用[入门])
色子问题
solr索引
.Solr构建索引-查询索引
solrj索引操作
Python资源索引 【转载】
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!