arrays - Julia,使用 findall 重置 3d 数组中的第三行

标签 arrays julia findall

我正在尝试使用 findall 获取一个一维数组的哪些元素大于第二个一维数组的元素的索引,然后使用这些索引设置第三个一维数组的相应值一维数组为 0。 MWE:

# create 3d array
a, b = [3;2;2], [4;3;2];
c = transpose(cat(a,b, dims = 2));
d, e = [1;2;3], [2;3;4];
f = transpose(cat(d,e, dims = 2));
g = cat(c, f, dims = 3);

g
2×3×2 Array{Int64,3}:
[:, :, 1] =
 3  2  2
 4  3  2

[:, :, 2] =
 1  2  3
 2  3  4

findall.(g[end,:,1] >= g[end-1,:,1]) 

并使用索引重置 g[end,:,2] 的元素,这样我最终会得到

g
2×3×2 Array{Int64,3}:
[:, :, 1] =
 3  2  2
 4  3  2

[:, :, 2] =
 1  2  3
 0  0  4

谢谢。 J

最佳答案

下面的代码给出了您请求的答案。您只是将 . 放在了错误的位置。您想要逐个元素比较 > 操作,然后将 findall 应用于整个结果数组(而不是逐个元素)。

julia> g[end, findall(g[end,:,1] .> g[end-1,:,1]), 2] .= 0
2-element view(::Array{Int64,3}, 2, [1, 2], 2) with eltype Int64:
 0
 0

julia> g
2×3×2 Array{Int64,3}:
[:, :, 1] =
 3  2  2
 4  3  2

[:, :, 2] =
 1  2  3
 0  0  4

但是,我不会尝试将所有数据编译成这样的一个大数组。使用三个独立的一维数组变量比在一个变量中使用三个维度更容易。再次使用上面的变量:

julia> e[b .> a] .= 0
2-element view(::Array{Int64,1}, [1, 2]) with eltype Int64:
 0
 0

julia> e
3-element Array{Int64,1}:
 0
 0
 4

关于arrays - Julia,使用 findall 重置 3d 数组中的第三行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65833961/

相关文章:

javascript - 如何使用Javascript基于动态数据合并多个对象

python - 难以理解正则表达式方法 findall

Python RE - finditer 和 findall 的不同匹配

php - 如何: remove object from array that has certain property value and crete new array as result

objective-c - 合并两个数组,同时保留原始数组顺序

javascript - 如何在 JavaScript 中创建多维数组?

matplotlib - Julia Plots : PyCall. PyError("PyImport_ImportModule\n\npyimport 找不到 Python 包 matplotlib.pyplot

latex - 将 LaTeX 方程转换为 SymPy [Julia] 格式

Julia 基于命名参数的多次调度?

Python 正则表达式 findall 交替行为