假设我们在Julia中具有以下3个数组:
Suppose we have the following 3 arrays in Julia:
5.0 3.5 6.0 3.6 7.0 3.0
5.0 3.5 6.0 3.6 7.0 3.0
5.0 4.5 6.0 4.7 8.0 3.0
5.0 4.5 6.0 4.7 8.0 3.0
5.0 4.0 6.0 3.2 8.0 4.0
5.0 4.0 6.0 3.2 8.0 4.0
我想通过第一列的公共值将一个数组中的3个数组合并,将第二列的值相加.结果必须为以下数组:
I want to merge the 3 arrays in one array, by common values of the first column, summing the values of the second column. The result must be the following array:
5.0 12 6.0 11.5 7.0 3.0 8.0 7.0
5.0 12 6.0 11.5 7.0 3.0 8.0 7.0
我尝试了vcat和reduce,但是没有得到伪装的结果.有没有一种相对简单的方法来编写指令,从而避免了耗时的代码?谢谢!
I tried vcat and reduce but I don't get the pretended result. Is there a relatively simple way to code the instructions, avoiding a time-consuming code? Thank you!
推荐答案给出以下两个假设:
然后对于大多数输入组合(即输入数组的数量,数组的大小),以下算法应利用以下假设,明显胜过其他答案:
then for most input combinations (i.e. number of input arrays, sizes of arrays), the following algorithm should significantly outperform the other answers by taking advantage of the assumptions:
function f_ag(x::Matrix{T}...)::Matrix{T} where {T<:Number} isempty(x) && error("Empty input") any([ size(y,2) != 2 for y in x ]) && error("Input matrices must have two columns") length(x) == 1 && return copy(x[1]) #simple case shortcut nxmax = [ size(y,1) for y in x ] nxarrinds = find(nxmax .> 0) nxrowinds = ones(Int, length(nxarrinds)) z = Tuple{T,T}[] while !isempty(nxarrinds) xmin = minimum(T[ x[nxarrinds[j]][nxrowinds[j], 1] for j = 1:length(nxarrinds) ]) minarrinds = Int[ j for j = 1:length(nxarrinds) if x[nxarrinds[j]][nxrowinds[j], 1] == xmin ] rowsum = sum(T[ x[nxarrinds[k]][nxrowinds[k], 2] for k in minarrinds ]) push!(z, (xmin, rowsum)) for k in minarrinds nxrowinds[k] += 1 end for j = length(nxarrinds):-1:1 if nxrowinds[j] > nxmax[nxarrinds[j]] deleteat!(nxrowinds, j) deleteat!(nxarrinds, j) end end end return [ z[n][j] for n = 1:length(z), j = 1:2 ] end如果违反了假设2,即不能保证第一列是唯一的,您仍然可以利用排序顺序,但是由于您需要另外查看,因此算法将变得更加复杂.转发每个最小索引以检查重复项.在这一点上,我不会让自己陷入痛苦之中.
If assumption 2 is violated, that is, the first column is not guaranteed to be unique, you can still take advantage of the sort order, but the algorithm is going to be more complicated again since you'll need to additionally look forward on each minimum index to check for duplicates. I'm not going to put myself through that pain at this point.
还请注意,您可以调整以下行:
Also note, you could adjust the following line:
rowsum = sum(T[ x[nxarrinds[k]][nxrowinds[k], 2] for k in minarrinds ])对此:
rowsum = input_func(T[ x[nxarrinds[k]][nxrowinds[k], 2:end] for k in minarrinds ])现在您可以输入所需的任何函数,并且在输入矩阵中还可以具有任意数量的附加列.
and now you can input whatever function you like, and also have any number of additional columns in your input matrices.
可能在这里可以添加一些其他优化,例如预分配z,只有两个输入矩阵时的专用例程等,但是我不会理会它们.
There are probably some additional optimizations that could be added here, eg pre-allocating z, specialized routine when there are only two input matrices, etc, but I'm not going to bother with them.
更多推荐
在Julia中按通用列值合并数组
发布评论