# [R] "ciclo for" in matrix construction ( matlab -> R )

3 messages
Open this post in threaded view
|
Report Content as Inappropriate

## [R] "ciclo for" in matrix construction ( matlab -> R )

 hi, i'm new to R i have a little script in matlab.... now i'm porting it to R but i've some problem. how can i write in R language this matlab/octave code: LX = L ;                     >>> L is a matrix ss = size(LX,1); E_N_2 = zeros(ss,2); for i = 1:ss E_2 = E_0 + LX(i,1)*C(3)+LX(i,2)*C(4);      >>>> E_0 , C(3), c(4)   are costant N_2 = N_0 + LX(i,2)*C(3)-LX(i,1)*C(4); r=[ E_2,N_2]; E_N_2(i,:)=r; end E_N_2 i'm try so : library(matlab) LX <- L ss <- dim(LX) ss <- ss[1] E_N_2 <- zeros(ss,2) for(i in 1:ss) ..... .....? thanks for any help :-) Massimo my mayor problem is to know how the "cicle for" works         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: [R] "ciclo for" in matrix construction ( matlab -> R )

 The help page for 'for' warns:       Note that it is a common mistake to forget to put braces ('{ ..       }') around your statements, e.g., after 'if(..)' or 'for(....)'.       In particular, you should not have a newline between '}' and       'else' to avoid a syntax error in entering a 'if ... else'       construct at the keyboard or via 'source'. For that reason, one       (somewhat extreme) attitude of defensive programming is to always       use braces, e.g., for 'if' clauses. Also this is illustrated in R FOR OCTAVE USERS   http://cran.r-project.org/doc/contrib/R-and-octave.txtin the section starting:   MULTILINE FOR STATEMENTS: On Wed, 4 Oct 2006, massimodisasha wrote: > hi, > i'm new to R > i have a little script in matlab.... > now i'm porting it to R but i've some problem. > how can i write in R language this > matlab/octave code: > > LX = L ;                     >>> L is a matrix > ss = size(LX,1); > E_N_2 = zeros(ss,2); > > for i = 1:ss > E_2 = E_0 + LX(i,1)*C(3)+LX(i,2)*C(4);      >>>> E_0 , C(3), c(4) > are costant > N_2 = N_0 + LX(i,2)*C(3)-LX(i,1)*C(4); > r=[ E_2,N_2]; > E_N_2(i,:)=r; > end > E_N_2 > > i'm try so : > > library(matlab) > LX <- L > ss <- dim(LX) > ss <- ss[1] > E_N_2 <- zeros(ss,2) > for(i in 1:ss) > ..... > .....? > > thanks for any help :-) > Massimo > > my mayor problem is to know how the "cicle for" works > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. > Charles C. Berry                        (858) 534-2098                                           Dept of Family/Preventive Medicine E mailto:[hidden email]         UC San Diego http://biostat.ucsd.edu/~cberry/         La Jolla, San Diego 92093-0717 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: [R] "ciclo for" in matrix construction ( matlab -> R )

 In reply to this post by Massimo Di Stefano massimodisasha wrote: > hi, > i'm new to R > i have a little script in matlab.... > now i'm porting it to R but i've some problem. > how can i write in R language this > matlab/octave code: > > LX = L ;                     >>> L is a matrix > ss = size(LX,1); > E_N_2 = zeros(ss,2); > > for i = 1:ss > E_2 = E_0 + LX(i,1)*C(3)+LX(i,2)*C(4);      >>>> E_0 , C(3), c(4)   > are costant > N_2 = N_0 + LX(i,2)*C(3)-LX(i,1)*C(4); > r=[ E_2,N_2]; > E_N_2(i,:)=r; > end > E_N_2 > > i'm try so : > > library(matlab) > LX <- L > ss <- dim(LX) > ss <- ss[1] > E_N_2 <- zeros(ss,2) > for(i in 1:ss) > ..... > .....? > > thanks for any help :-) > Massimo > > my mayor problem is to know how the "cicle for" works Perhaps a more important problem is learning how to use matrix algebra.  Loops are notoriously slow in R, and worth avoiding in octave as well. E_0 <- 12 N_0 <- 13 C3 <- 21 C4 <- 22 L <- matrix(c(1,2,3,4,5,6,7,8,9),ncol=3) ss <- dim(L)[1] LX <- L[1:ss,1:2] C <- matrix(c(C3,C4,-C4,C3),ncol=2) EN <- matrix(rep(c(E_0,N_0),ss),ncol=2,byrow=TRUE) E_N_2 <- EN + LX %*% C E_N_2 -- --------------------------------------------------------------------------- Jeff Newmiller                        The     .....       .....  Go Live... DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...                                        Live:   OO#.. Dead: OO#..  Playing Research Engineer (Solar/Batteries            O.O#.       #.O#.  with /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.