Hi all,
I was trying to optimize my naive code I wrote for an algorithm I’m working on, and I’ve found this part takes up the most runtime:
num_of_the_same_columns = 0
num_of_columns = size(matrix, 2)
for column_idx in 1:num_of_columns
for next_column_idx in (column_idx + 1):num_of_columns
if all(matrix[:, column_idx] .== matrix[:, next_column_idx])
num_of_the_same_columns += 1
end
end
end
What it is trying to do is to count columns in a matrix, which have the same values in corresponding cells, e.g. here:
[1, 1, 1, 1, 0, 0]
[1, 1, 1, 0, 1, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
columns 1, 2, 3 are “the same” so the num_of_the_same_columns should add up to 3.
Is there a more efficient way to search for something like this in Julia than these nasty nested loops?