Search code examples
arraysstringjuliavectorizationbroadcast

Julia pairwise broadcast


I would like to compare every pair of strings in a list of strings in Julia. One way to do it is

equal_strs = [(x == y) for x in str_list, y in str_list]

However, if I use broadcast as follows:

equal_strs = broadcast(==, str_list, str_list)

it returns a vector instead of a 2D array. Is there a way to output a 2D array using broadcast?


Solution

  • Broadcasting works by expanding ("broadcasting") dimensions that do not have the same length, in a way such that an array with (for example) size Nx1xM broadcasted with a NxKx1 gives an NxKxM array.

    This means that if you broadcast an operation with to length N vectors, you will get a length N vector.

    So you need one string array to be a length N vector, and the other an 1xM matrix:

    julia> using Random
    
    julia> str1 = [randstring('A':'C', 3) for _ in 1:5]
    5-element Vector{String}:
     "ACC"
     "CBC"
     "AAC"
     "CAB"
     "BAB"
    
    1.8.0> str2 = [randstring('A':'C', 3) for _ in 1:4]
    4-element Vector{String}:
     "ABB"
     "BAB"
     "CAA"
     "BBC"
    
    1.8.0> str1 .== permutedims(str2)
    5×4 BitMatrix:
     0  0  0  0
     0  0  0  0
     0  0  0  0
     0  0  0  0
     0  1  0  0
    

    permutedims will change a length N vector into a 1xN matrix.

    BTW, you would very rarely use broadcast in your code (broadcast(==, a, b)), instead, use the dot syntax, a .== b, which is more idiomatic.