title: Matrix Error in R
body: <p>I'm writing a function in R to take a number and perform an operation on every row of the matrix. Each matrix should have 50 elements, <code>ncol(x)</code> is 50. Here's the calculation I'm performing </p>
  
  <pre><code>function(v1,v2){
   a &lt;- sum(v1*v2)
   b &lt;- sqrt(sum(v1*v1))* sqrt(sum(v2*v2))
  return(a/b)
  }
  </code></pre>
  
  <p>Here's a sample of my data:</p>
  
  <pre><code>the 0.41800  0.249680 -0.41242  0.121700 0.345270 -0.044457 -0.49688 -0.178620 -0.00066023 -0.656600 0.278430 -0.14767 -0.55677  0.14658 -0.0095095
  . 0.15164  0.301770 -0.16763  0.176840 0.317190  0.339730 -0.43478 -0.310860 -0.44999000 -0.294860 0.166080  0.11963 -0.41328 -0.42353  0.5986800
  of  0.70853  0.570880 -0.47160  0.180480 0.544490  0.726030  0.18157 -0.523930    0.10381000 -0.175660 0.078852 -0.36216 -0.11829 -0.83336  0.1191700
  to  0.68047 -0.039263  0.30186 -0.177920 0.429620  0.032246 -0.41376  0.132280 -0.29847000 -0.085253 0.171180  0.22419 -0.10046 -0.43653  0.3341800
  and 0.26818  0.143460 -0.27877  0.016257 0.113840  0.699230 -0.51332 -0.473680 -0.33075000 -0.138340 0.270200  0.30938 -0.45012 -0.41270 -0.0993200
  in  0.33042  0.249950 -0.60874  0.109230 0.036372  0.151000 -0.55083 -0.074239 -0.09230700 -0.328210 0.095980 -0.82269 -0.36717 -0.67009  0.4290900`
  </code></pre>
  
  <p>Here's what I'm trying to write, it's pretty straightforward:</p>
  
  <pre><code>function(w,m) {
   r=matrix(nrow=nrow(m),ncol=2)
   for(i in 1:(nrow(m)-1)) {
   r[i,1]=cosineSim(m[w,],m[i,])
  }
   r=sort(r,decending=TRUE)
   return (r)
  }
  </code></pre>
  
  <p>1)Create a new matrix with the same columns and rows as matrix m. 
  2)Loop through m-1 rows. 
  3)Each row, use <code>cosineSim</code> to compute the cosine similarity of the current vector (row) of the loop.
  4)Once the end of the loop is finished, sort and return <code>r</code> which is the array holding the result. </p>
  
  <p>The problem is that the algorithm seems to enter an infinite loop.</p>
  
  <p>I need this to work for a project and would very much appreciate any help.</p>