Skip to content
Prev 388461 / 398513 Next

Some difficulties to use the apply command on an array

Use a different indexing strategy:

res <- apply(x, 1, FUN = function(x) diag( svd(x)$d ))

The result is flattened but each of the columns in that `res`-object are the entries in a 3x3 matrix. Reform it by:
, , 1

         [,1]     [,2]      [,3]
[1,] 15.73833 0.000000 0.0000000
[2,]  0.00000 1.502771 0.0000000
[3,]  0.00000 0.000000 0.1049992

, , 2

        [,1]     [,2]     [,3]
[1,] 16.0011 0.000000 0.000000
[2,]  0.0000 3.357086 0.000000
[3,]  0.0000 0.000000 0.486643

, , 3

         [,1]     [,2]    [,3]
[1,] 16.05269  0.00000 0.00000
[2,]  0.00000 11.49007 0.00000
[3,]  0.00000  0.00000 1.68421

, , 4

         [,1]     [,2]     [,3]
[1,] 19.58508  0.00000 0.000000
[2,]  0.00000 14.83712 0.000000
[3,]  0.00000  0.00000 3.139307

, , 5

         [,1]     [,2]     [,3]
[1,] 26.75931  0.00000 0.000000
[2,]  0.00000 11.81372 0.000000
[3,]  0.00000  0.00000 5.582891