I have a vector, v
. I am trying to construct a matrix whose elements M[i][j]
depend on v[i]
and v[j]
.
The 'loop' way of performing this is the following (for example):
v = c(1:4)
M = matrix(0, nrow = length(v), ncol = length(v))
for(i in 1:length(v)){
for(j in i:length(v)){
M[i,j] = v[i] * v[j]
}
}
M
then looks like:
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 0 4 6 8
[3,] 0 0 9 12
[4,] 0 0 0 16
Where the matrix can now be easily symmetrised.
However, this become prohibitively slow when v
becomes large. Considering how embarrassingly parallel this problem is, I was hoping there was a way of performing this task in parallel?
NB - The actual function I am performing on the elements of the vector is more complicated than multiplication, but that's just a decent example!