简体   繁体   English

4D数组中最小/最大元素的索引

[英]Indexes of min/max elements in 4D array

I need to change min/max values of a 4D array along specific axis. 我需要沿特定轴更改4D数组的最小/最大值。 The coordinates of min/max along a particular axis are retuned by armax , so for 4D array a have a 3D array. 沿着特定轴的最小/最大坐标由armax调整,因此对于4D数组,其具有3D数组。

Now from this 3D array I need to recover the full index of min/max values. 现在,我需要从3D阵列中恢复最小/最大值的完整索引。 I understand the most efficient way to do it is combining results of mgrid and argmin using something like (for min along 1st axis): 我知道最有效的方法是使用类似(对于第1轴的argmin来组合mgridargmin结果:

mg = np.mgrid[0:Array.shape[0], 0:Array.shape[1], 0:Array.shape[2], 0:Array.shape[3]]

Array[mg[0], np.argmin(Array, axis=1), mg[2], mg[3]] = np.min(Array)

however, this does not seem to work correctly. 但是,这似乎无法正常工作。

Could someone please advise on how to correct the code above to replace the min values of Array along 1st axis with absolute min of Array? 有人可以建议如何更正上面的代码,以用数组的绝对最小值替换沿第一个轴的数组的最小值吗?

Alternatively, if this is not the fastest way to do it, what is the optimal solution? 或者,如果这不是最快的方法,那么最佳解决方案是什么?

Try indexing with: 尝试使用以下方法建立索引:

Array[:, np.argmin(Array, axis=1), :, :] = ....

I think you are trying to set all the values of one 'plane' to a minimum value. 我认为您正在尝试将一个“平面”的所有值设置为最小值。 : is a slice , specifying all indexes along that axis. :是一个slice ,指定沿该轴的所有索引。

I haven't actually tried to run your example, but I don't think you need to generate mg for this purpose. 我实际上并没有尝试运行您的示例,但是我认为您不需要为此生成mg

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM