Laurent Rhelp
2021-Jul-05 15:38 UTC
[R] Some difficulties to use the apply command on an array
ok, I use the R version 4.0.3 I have to upgrade my version https://developer.r-project.org/blosxom.cgi/R-devel/NEWS/2021/03/06#n2021-03-06 Le 05/07/2021 ? 17:05, Andrew Simmons a ?crit?:> Hello, > > > It seems as though you may be using `MARGIN` incorrectly. `MARGIN` is > the indices you are looping through, so it should be MARGIN = 1, not > MARGIN = c(2, 3). Additionally, you probably want to set argument > `simplify` to FALSE. Something?like > > apply(x, 1, FUN = function(x) diag( svd(x)$d ), simplify = FALSE) > > seems to work well for me. I hope this helps! > > On Mon, Jul 5, 2021 at 10:56 AM Laurent Rhelp <LaurentRHelp at free.fr > <mailto:LaurentRHelp at free.fr>> wrote: > > Dear R-Help, > > I have an array x made up of three matrices of 5 rows and 3 > columns of > complex numbers (the complex numbers are not the problem) > > ## my array > x <- structure(c(5.6196790161893828+0i, 5.7565523942393364+0i, > 8.5242834298729342+0i, > ?????????????? 10.304766710160479+0i, 11.412967010108229+0i, > -2.6952197604866495-5.7324226520260237e-18i, > ?????????????? -3.8053698833683476-1.5535793240880411i, > -4.440850591952894-0.21277190153473785i, > ?????????????? -4.9049897326853316+0.22487294128201613i, > -4.6982778900868931-0.35986943359186585i, > ?????????????? -6.7722948827866034-1.8252881225149525e-16i, > -5.6864255243941733-0.82149125972147463i, > ?????????????? -2.5321753317962115-3.4309121438578525i, > 0.85377696343414344-4.3720244641138883i, > ?????????????? 6.3363344379954638-2.23654533694011i, > -2.6952197604866495+5.7324226520260237e-18i, > ?????????????? -3.8053698833683476+1.5535793240880411i, > -4.440850591952894+0.21277190153473785i, > ?????????????? -4.9049897326853316-0.22487294128201613i, > -4.6982778900868931+0.35986943359186585i, > ?????????????? 3.1683154084671417+0i, 5.397392162921844+0i, > 8.3085887222115922+0i, > ?????????????? 11.150391891976289+0i, 13.173434845070231+0i, > 3.7978840709983359+5.0735539531499429e-17i, > ?????????????? 3.0599215005995717-2.3669737981128267i, > 0.5951827523696811-3.2116907301709845i, > ?????????????? -1.7735244105472532-3.8366652813316566i, > -5.0558348297797169-2.6645747173403049i, > ?????????????? -6.7722948827866034+1.8252881225149525e-16i, > -5.6864255243941733+0.82149125972147463i, > ?????????????? -2.5321753317962115+3.4309121438578525i, > 0.85377696343414344+4.3720244641138883i, > ?????????????? 6.3363344379954638+2.23654533694011i, > 3.7978840709983359-5.0735539531499429e-17i, > ?????????????? 3.0599215005995717+2.3669737981128267i, > 0.5951827523696811+3.2116907301709845i, > ?????????????? -1.7735244105472532+3.8366652813316566i, > -5.0558348297797169+2.6645747173403049i, > ?????????????? 8.5581082281979697+0i, 8.6908832216086331+0i, > 12.394094469562258+0i, > ?????????????? 16.106350896659897+0i, 19.569513600539693+0i), .Dim > c(5L, 3L, > 3L)) > > str(x) > #? cplx [1:5, 1:3, 1:3] 5.62+0i 5.76+0i 8.52+0i ... > > The dimensions (i,j,k) of my array are 5, 3, 3. For every value of > i, I > am interested in the squared matrices made up of the j columns for > all > the k indice. For a given value i I have to keep the row i for > every k. > For example if i=1, I am interested by the matrix x[1,,] > ## > x[1,,] > > ##? ??????????? [,1]???????? [,2]???????? [,3] > ## [1,]? 5.619679+0i -2.695220+0i -6.772295+0i > ## [2,] -2.695220-0i? 3.168315+0i? 3.797884-0i > ## [3,] -6.772295-0i? 3.797884+0i? 8.558108+0i > > Now, for every value of i, I want to calculate the SVD > decomposition and > get the diagonal matrix created by the singular values. When I set > the i > value it works: > diag( svd(x[1,,])$d ) > > # [,1]???? [,2]????? [,3] > # [1,] 15.73833 0.000000 0.0000000 > # [2,]? 0.00000 1.502771 0.0000000 > # [3,]? 0.00000 0.000000 0.1049992 > > > But when I try to do a loop on the i value it doesn't work: > > apply(x, c(2,3), FUN = function(x) diag( svd(x)$d )) > > > # [,1]??????? [,2]??????? [,3] > # [1,] Numeric,361 Numeric,81? Numeric,144 > # [2,] Numeric,81? Numeric,400 Numeric,81 > # [3,] Numeric,144 Numeric,81? Numeric,900 > > I do not understand how to do the apply on an array in order to > get my > diagonals matrices. I think that I miss something about the > understanding of the array object. > > Thank you for your help > > Laurent > > > > > -- > L'absence de virus dans ce courrier ?lectronique a ?t? v?rifi?e > par le logiciel antivirus Avast. > https://www.avast.com/antivirus <https://www.avast.com/antivirus> > > ______________________________________________ > R-help at r-project.org <mailto:R-help at r-project.org> mailing list -- > To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > <https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- L'absence de virus dans ce courrier ?lectronique a ?t? v?rifi?e par le logiciel antivirus Avast. https://www.avast.com/antivirus [[alternative HTML version deleted]]