Hi, I think it was not your English, but the code that was a little hard to get.
I would suggest @sync @distributed
+ SharedArrays
or Threads.@threads
in front of the for i=1:size(a, 2)
loop. Please, try both and see which one works best for you.
I can see your idea, but my reading ability in the code is not very strong.
Many boolean expressions uncommented, sorry. The comments would be:
# True if the element b[2, j] is in a[:, i].
is_2 = a[1, i] == b[2, j] || a[2, i] == b[2, j] || a[3, i] == b[2, j]
...
# True if the elements of a[:, i] are contained in {b[1, j], b[3, j], b[4, j]}, no matter the order.
is_134 = is_1 & is_3 & is_4
...
# The element b[2, j] is not in a[:, i], so write b[2, j] in c.
elseif is_134
c[tmp, i] = j
c[tmp+2, i] = b[2, j]
tmp += 1
I would tell you to try to make the code you asked for now that I explained the code a bit more. The code you want is very similar to the previous one.
In case you still don’t know how to make it here it is
using BenchmarkTools
function foo(a, b)
c = zeros(Int, 2, size(a, 2))
@inbounds for i=1:size(a, 2)
for j=1:size(b, 2)
# True if the element b_{1, j} is in the ith row of a.
is_1 = a[1, i] == b[1, j] || a[2, i] == b[1, j] || a[3, i] == b[1, j]
# True if the element b_{2, j} is in the ith row of a.
is_2 = a[1, i] == b[2, j] || a[2, i] == b[2, j] || a[3, i] == b[2, j]
# True if ...
is_3 = a[1, i] == b[3, j] || a[2, i] == b[3, j] || a[3, i] == b[3, j]
is_4 = a[1, i] == b[4, j] || a[2, i] == b[4, j] || a[3, i] == b[4, j]
# This line could potentially save time with larger matrix sizes.
#!is_1 & !is_2 & !is_3 & !is_4 && continue
# True if the ith row of a is distributed in the first three elements of b[:, j]
is_123 = is_1 & is_2 & is_3
# True if the ith row of a is distributed in the elements: b[1, j], b[2, j], b[4, j].
is_124 = is_1 & is_2 & is_4
# True if the ith row of a is distributed in the elements: b[1, j], b[3, j], b[4, j].
is_134 = is_1 & is_3 & is_4
# True if ...
is_234 = is_2 & is_3 & is_4
if is_123
c[1, i] = j
c[2, i] = b[4, j]
elseif is_124
c[1, i] = j
c[2, i] = b[3, j]
elseif is_134
c[1, i] = j
c[2, i] = b[2, j]
elseif is_234
c[1, i] = j
c[2, i] = b[1, j]
end
end
end
return c
end
a = [1 2 3; 4 5 6; 7 8 9];
b = [1 4 3 2; 7 2 9 8; 4 6 1 5; 8 2 1 7; 11 3 4 8];
aT = Matrix(transpose(a))
bT = Matrix(transpose(b))
c = foo(aT, bT)
display(hcat(a, c'))
@btime foo($aT, $bT);