I have the following matrix:
        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
   [1,] 0 0 0 0 0 0 0 0 0  0  0  1  0  0  0  0  1  0  0  0  0  0  0  0
   [2,] 0 0 0 0 0 0 0 0 0  1  0  0  0  0  0  1  0  0  0  0  0  0  0  1
My aim is thus: For each row in the matrix, I wish to print the column numbers where the value in the matrix is 1.
The following works, sort of:
for(l in 1:nrow(matrix))
{
print(which(matrix[l,]==1))
}
But returns the columns twice:
12 17
12 17
10 16 24
10 16 24
Is there a way to have the appropriate column numbers returned only a single time?
 
    