Indexing during assignment <p>Say I have this sample data</p> <pre><code>A = 1.0000 6.0000 180.0000 12.0000 1.0000 5.9200 190.0000 11.0000 1.0000 5.5800 170.0000 12.0000 1.0000 5.9200 165.0000 10.0000 2.0000 5.0000 100.0000 6.0000 2.0000 5.5000 150.0000 8.0000 2.0000 5.4200 130.0000 7.0000 2.0000 5.7500 150.0000 9.0000 </code></pre> <p>I wish to calculate the variance of each column, grouped by class (the first column).</p> <p>I have this working with the following code, but it uses hard coded indices, requiring knowledge of the number of samples per class and they must be in specific order. </p> <p>Is there a better way to do this?</p> <pre><code>variances = zeros(2,4); variances = [1.0 var(A(1:4,2)), var(A(1:4,3)), var(A(1:4,4)); 2.0 var(A(5:8,2)), var(A(5:8,3)), var(A(5:8,4))]; disp(variances); 1.0 3.5033e-02 1.2292e+02 9.1667e-01 2.0 9.7225e-02 5.5833e+02 1.6667e+00 </code></pre>