Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating matrix and variance after loop
    primarykey
    data
    text
    <p>I have an array with an 8X11 matrix called results2 and I'm trying to do two things iteratively (~1000 times). </p> <ol> <li>randomly select 4 cells at a time in the matrix and either add or subtract a value to that selection. The values need to be added either this way (+, -, -, +) or this way (-,+,+,-). After doing this addition/subtraction, the 8X11 matrix should have the updated modification done on it and the process should repeat. </li> <li>calculate and update the variance for each cell in the 8X11 matrix after every loop and save it into a vector. In the end there should 88 vectors with a variance value inside it--1 for each cell in the matrix.</li> </ol> <p>What I have so far is: </p> <pre><code>r2 &lt;- results[2,,] dat &lt;- r2[sample(prod(dim(r2)),4)] #randomly samples from the 8X11 matrix dat &lt;- as.matrix(dat) sampleRand &lt;- function(dat) { x_min = seq(0,min(sample(dat,4)), by=0.01) #random sampling for 4 cells and finds range from 0 to the min of that sample x.min = sample(x_min,1) # randomly selects the min to use to add or subtract by flip = sample(0:1,1,rep=T) #if flip is = 1 then do +,-,-,+ if flip = 0 then do -,+,+,- if(flip &gt; 0) { dat[1,1] = dat[1,1] + x.min dat[2,1] = dat[2,1] - x.min dat[3,1] = dat[3,1] - x.min dat[4,1] = dat[4,1] + x.min if (dat[1,1] &lt; 0 || dat[2,1] &lt; 0 || dat[3,1] &lt; 0 || dat[4,1] &lt; 0) { print("FALSE") } } else if(flip == 0) { dat[1,1] = dat[1,1] - x.min dat[2,1] = dat[2,1] + x.min dat[3,1] = dat[3,1] + x.min dat[4,1] = dat[4,1] - x.min } else if (dat[1,1] &lt; 0 || dat[2,1] &lt; 0 || dat[3,1] &lt; 0 || dat[4,1] &lt; 0) { print("FALSE") } } </code></pre> <p>The problem with the above code (aside from ugliness) is that I am selecting out values from the 8X11 matrix and modifying them. I am not modifying them directly in the 8X11 matrix. How can I do this?</p> <p>Also, how can I calculate the variance for each cell in the 8X11 matrix and have the variance value update for that cell after each iteration?. Ideally, this variance value would be saved as it's own vector. So in the end there should be 88 vectors with a variance value in each. </p> <p>thanks.</p> <p>By the way, results is just a 3D array with 13,000 matrices that are 8X11 each. An example of matrix #2 in results is: </p> <pre><code>results[2,,] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 0.1108446 0.09536505 0.6639205 0.5750731 0.3526045 0.5534660 0.8835108 [2,] 0.2762736 0.16588542 0.5556568 0.6600122 0.4832881 0.4584993 1.0334293 [3,] 0.1313672 0.00000000 0.5799588 0.5835488 0.7482058 0.5714304 0.8910430 [4,] 0.1815236 0.50803010 0.4826228 0.7011605 0.5161530 0.3057819 0.9230045 [5,] 0.1363721 0.77955695 0.5983714 0.5894578 0.5964733 0.8284053 0.7949902 [6,] 0.2058215 0.54171493 0.3260954 0.6145317 0.5241678 1.1855122 0.9472204 [7,] 0.1398614 0.62403905 0.5403183 0.5141039 0.6353840 0.8727070 0.5891284 [8,] 0.1546674 0.54992965 0.8204701 0.6335292 0.5297555 0.9809781 0.6295093 [,8] [,9] [,10] [,11] [1,] 0.9334862 0.7306472 0.6472618 1.0034951 [2,] 0.6672198 0.4258317 0.6736558 0.9045543 [3,] 0.7593824 0.5955351 0.5679406 1.0567393 [4,] 0.6764162 0.8904367 0.6936535 0.9122004 [5,] 0.8040301 0.6321197 0.3979384 0.9446451 [6,] 0.6828278 0.7215308 0.8486158 0.8146954 [7,] 0.7100341 0.6410416 1.2111213 0.9692271 [8,] 0.6010656 0.7648455 0.8935872 0.6057215 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload