Re: [R] converting MATLAB -> R | element-wise operation
> Berwin A Turlach > on Wed, 28 Feb 2024 17:42:27 +0800 writes: > On Tue, 27 Feb 2024 13:51:25 -0800 Jeff Newmiller via > R-help wrote: >> The fundamental data type in Matlab is a matrix... they >> don't have vectors, they have Nx1 matrices and 1xM >> matrices. > Also known as column vectors and row vectors. :) >> Vectors don't have any concept of "row" vs. "column". > They do in (numerical) linear algebra. And MATLAB was > written by numerical analysts for numerical analysts. :-) > So they distinguish between row and column vectors. > GAUSS also distinguishes between row and column vectors. There are *vectors* and they belong to a vector space and that's it. (and yes, arrays of a given rank and dimensions also are elements of a vector space, and hence matrices are, and then indeed, a 3x1 matrix and a 1x3 matrix are *not* members of the same vector space -- even though the two vector spaces are isomorphic to each other) The matlab induced --> applied linear algebra / math "column- and row-vectors" have been convenient because then everything is a matrix (or scalar), and that fits well with a product called "matlab" which is short for "matrix lab(oratory)". BUT they are mathematically confusing and therefore in my view very undesirable terminology. I'm very very strongly with Jeff that "the only" consistent language would be to call them 1-row matrix and 1-column matrix, because that's what they are, well defined and perfect within the well defined math realm of linear algebra. Yes, matrix-vector calculus , scalar products, bilinear forms, ... can be generalized in such ways that 1-row matrices and 1-column matrices can be treated equivalently to vectors and vice versa .. and that is sometimes convenient. Still, the "column-vector" and "row-vector" terms have lead to much confusion in my view, because really Nx1 or 1xM are dimensions of matrices and *not* of any vectors .. > R (and S) does not distinguish between row and column > vectors, and is in this aspect quite unique among the > groups of vector-oriented programming languages (AFAICT). I agree. And that's good -- because "vectors are vectors" aka "a rank-1 array is a rank-1 array is a rank-1 array" (Richard O'Keefe) > But treating every vector as a column vector, together > with the recycling rule, allows for the easy coding of the > matrix/vector calculations that one (mostly) comes across > in statistical computing. For the rare occasion when this > is not true the sweep() command is provided, typically > remembered once one was bitten by the lack of distinction > between row and column vectors. :) > Cheers, > Berwin Martin __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] converting MATLAB -> R | element-wise operation
The first vector-oriented programming language I ever learned or used was APL, and APL makes *no* distinction between row vectors and column vectors. It has rank-0 (scalar), rank-1 (vector), rank-2 (matrix), rank-3 ... and so on arrays. A rank-1 array is a rank-1 array is a rank-1 array and there is an algebra of arrays to go with it. I learned PL/I around the same time, which also has array expressions (but at least back then it did not have matrix product). Again, a 1D array is a 1D array is a 1D array. Since 1990, Fortran has been an array-processing language, and there has never been any distinction between row vectors and column vectors in Fortran and isn't any such distinction now. One thing that these three languages have in common is the view that a rank-N array and a rank-(N+1) array are not the same kind of thing. I can understand MATLAB distinguishing between row and column vectors, although nothing in my undergraduate numerical analysis education prepared me to expect or even like it. But from an APL perspective it is confusing and limiting. And for that matter S and R are confusing in their own way. But to call R's approach "unique" within the family of array-oriented programming languages is an exaggeration. The BASIS system from Lawrence Livermore does not make any distinction between row and column vectors; a 1D array is a 1D array is a 1D array. Most notably in modern times, Julia belongs to the a-1D-array-is-a-1D-array-is-a-1D-array and a-1D-array-is-not-a-2D-array camp. As one person writing ina Julia thread put it, Back when I wrote everything in Matlab, those years were characterized by a series of mild annoyances of always having to worry about whether a was a row vector or a column vector, and sprinkling a(:) and a(:)' like pixie dust to fix problems. It’s a huge relief to have real one-dimensional objects. On Wed, 28 Feb 2024 at 22:43, Berwin A Turlach wrote: > > On Tue, 27 Feb 2024 13:51:25 -0800 > Jeff Newmiller via R-help wrote: > > > The fundamental data type in Matlab is a matrix... they don't have > > vectors, they have Nx1 matrices and 1xM matrices. > > Also known as column vectors and row vectors. :) > > > Vectors don't have any concept of "row" vs. "column". > > They do in (numerical) linear algebra. And MATLAB was written by > numerical analysts for numerical analysts. :-) So they distinguish > between row and column vectors. > > GAUSS also distinguishes between row and column vectors. > > R (and S) does not distinguish between row and column vectors, and is > in this aspect quite unique among the groups of vector-oriented > programming languages (AFAICT). But treating every vector as a column > vector, together with the recycling rule, allows for the easy coding of > the matrix/vector calculations that one (mostly) comes across in > statistical computing. For the rare occasion when this is not true the > sweep() command is provided, typically remembered once one was bitten > by the lack of distinction between row and column vectors. :) > > Cheers, > > Berwin > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] converting MATLAB -> R | element-wise operation
Agree that sweep is the tool here. (If you think it is clunky, check how more general array-sweep operations can be done in Matlab.) However, it isn't really true that sweep isn't moving things around. Notice the call to aperm() at the end of the code for sweep(): perm <- c(MARGIN, seq_along(dims)[-MARGIN]) FUN(x, aperm(array(STATS, dims[perm]), order(perm)), ...) What this essentially does for "our" case is > rbind(1:3,4:6)/t(matrix(c(2,3,4), 3,2)) [,1] [,2] [,3] [1,] 0.5 0.667 0.75 [2,] 2.0 1.667 1.50 I.e. take the matrix, create the divisor by replicating STATS to form a matrix of same size. This is easier if the MARGIN indices come first, because the recycling works. So you get the 2x3 matrix by filling a 3x2 and then transposing it (aperm() does this more generally). Finally, just call FUN on the two arrays. -pd > On 27 Feb 2024, at 22:51 , Jeff Newmiller via R-help > wrote: > > Why anything but sweep? > > The fundamental data type in Matlab is a matrix... they don't have vectors, > they have Nx1 matrices and 1xM matrices. > > Vectors don't have any concept of "row" vs. "column". Straight division is > always elementwise with recycling as needed, and matrices are really vectors > in row-major order: > > 1 2 3 > 4 5 6 > > is really > > 1 4 2 5 3 6 > > and when you do straight division NN / lambda then lambda is repeated: > > 1 4 2 5 3 6 > 2 3 4 2 3 4 > > to get > > 0.5 1.3 0.5 2.5 1.0 1.5 > > but if you transpose first > > 1 4 > 2 5 > 3 6 > > then that corresponds to an underlying vector: > > 1 2 3 4 5 6 > > which lines up with lambda in t(NN)/lambda as: > > 1 2 3 4 5 6 > 2 3 4 2 3 4 > > to obtain: > > 0.50 0.67 0.75 2.0 1.67 1.50 > > and inherits the dimensions of t(NN): > > 0.50 2.00 > 0.67 1.67 > 0.75 1.50 > > which can be transposed back as in t( t( NN ) / lambda ): > > 0.50 0.67 0.75 > 2.00 1.67 1.50 > > but that requires a lot of moving elements around while sweep does not. > > Operators are not necessarily "better" than named functions... they just look > different. > > > On February 27, 2024 11:54:26 AM PST, Evan Cooch wrote: >> So, trying to convert a very long, somewhat technical bit of lin alg >> MATLAB code to R. Most of it working, but raninto a stumbling block that >> is probaably simple enough for someone to explain. >> >> Basically, trying to 'line up' MATLAB results from an element-wise >> division of a matrix by a vector with R output. >> >> Here is a simplified version of the MATLAB code I'm translating: >> >> NN = [1, 2, 3; 4, 5, 6]; % Example matrix >> lambda = [2, 3, 4]; % Example vector >> result_matlab = NN ./ lambda; >> >> which yields >> >> 0.5 0.7 0.75000 >> 2.0 1.7 1.5 >> >> >> So, the only way I have stumbled onto in R to generate the same results >> is to use 'sweep'. The following 'works', but I'm hoping someone can >> explain why I need something as convoluted as this seems (to me, at least). >> >> NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Example matrix >> lambda <- c(2, 3, 4) # Example vector >> sweep(NN, 2, lambda, "/") >> >> >> [,1] [,2] [,3] >> [1,] 0.5 0.667 0.75 >> [2,] 2.0 1.667 1.50 >> >> First tried the more 'obvious' NN/lambda, but that yields 'the wrong >> answer' (based solely on what I'm trying to accomplish): >> >> >>[,1] [,2] [,3] >> [1,] 0.50 0.5 1.0 >> [2,] 1.33 2.5 1.5 >> >> So, why 'sweep'? >> >> [[alternative HTML version deleted]] >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > -- > Sent from my phone. Please excuse my brevity. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] converting MATLAB -> R | element-wise operation
On Tue, 27 Feb 2024 14:54:26 -0500 Evan Cooch wrote: > So, trying to convert a very long, somewhat technical bit of lin alg > MATLAB code to R. Most of it working, but raninto a stumbling block > that is probaably simple enough for someone to explain. On https://cran.r-project.org/other-docs.html the documents: “Matlab® / R Reference” by David Hiebeler (PDF, 2010-05-25, 52 pages) and “R and Octave” by Robin Hankin (Text), a reference sheet translating between the most common Octave (or Matlab) and R commands might be useful. IIRC, I once used them when I had to do something in Matlab/Octave and used them as reverse-lookup (I know how to do it in R, how is it done in Matlab??). :-) Cheers, Berwin __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] converting MATLAB -> R | element-wise operation
On Tue, 27 Feb 2024 13:51:25 -0800 Jeff Newmiller via R-help wrote: > The fundamental data type in Matlab is a matrix... they don't have > vectors, they have Nx1 matrices and 1xM matrices. Also known as column vectors and row vectors. :) > Vectors don't have any concept of "row" vs. "column". They do in (numerical) linear algebra. And MATLAB was written by numerical analysts for numerical analysts. :-) So they distinguish between row and column vectors. GAUSS also distinguishes between row and column vectors. R (and S) does not distinguish between row and column vectors, and is in this aspect quite unique among the groups of vector-oriented programming languages (AFAICT). But treating every vector as a column vector, together with the recycling rule, allows for the easy coding of the matrix/vector calculations that one (mostly) comes across in statistical computing. For the rare occasion when this is not true the sweep() command is provided, typically remembered once one was bitten by the lack of distinction between row and column vectors. :) Cheers, Berwin __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] converting MATLAB -> R | element-wise operation
... and here is a more or less direct translation of the Matlab code that should now be obvious given your previous responses: > m <- matrix(1:6, nr=2, byrow = TRUE) ## Matlab order > m [,1] [,2] [,3] [1,]123 [2,]456 > sweep(m, 2, 2:4, "/") [,1] [,2] [,3] [1,] 0.5 0.667 0.75 [2,] 2.0 1.667 1.50 Cheers, Bert On Tue, Feb 27, 2024 at 1:03 PM Evan Cooch wrote: > So, trying to convert a very long, somewhat technical bit of lin alg > MATLAB code to R. Most of it working, but raninto a stumbling block that > is probaably simple enough for someone to explain. > > Basically, trying to 'line up' MATLAB results from an element-wise > division of a matrix by a vector with R output. > > Here is a simplified version of the MATLAB code I'm translating: > > NN = [1, 2, 3; 4, 5, 6]; % Example matrix > lambda = [2, 3, 4]; % Example vector > result_matlab = NN ./ lambda; > > which yields > > 0.5 0.7 0.75000 > 2.0 1.7 1.5 > > > So, the only way I have stumbled onto in R to generate the same results > is to use 'sweep'. The following 'works', but I'm hoping someone can > explain why I need something as convoluted as this seems (to me, at least). > > NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Example matrix > lambda <- c(2, 3, 4) # Example vector > sweep(NN, 2, lambda, "/") > > > [,1] [,2] [,3] > [1,] 0.5 0.667 0.75 > [2,] 2.0 1.667 1.50 > > First tried the more 'obvious' NN/lambda, but that yields 'the wrong > answer' (based solely on what I'm trying to accomplish): > > > [,1] [,2] [,3] > [1,] 0.50 0.5 1.0 > [2,] 1.33 2.5 1.5 > > So, why 'sweep'? > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] converting MATLAB -> R | element-wise operation
Why anything but sweep? The fundamental data type in Matlab is a matrix... they don't have vectors, they have Nx1 matrices and 1xM matrices. Vectors don't have any concept of "row" vs. "column". Straight division is always elementwise with recycling as needed, and matrices are really vectors in row-major order: 1 2 3 4 5 6 is really 1 4 2 5 3 6 and when you do straight division NN / lambda then lambda is repeated: 1 4 2 5 3 6 2 3 4 2 3 4 to get 0.5 1.3 0.5 2.5 1.0 1.5 but if you transpose first 1 4 2 5 3 6 then that corresponds to an underlying vector: 1 2 3 4 5 6 which lines up with lambda in t(NN)/lambda as: 1 2 3 4 5 6 2 3 4 2 3 4 to obtain: 0.50 0.67 0.75 2.0 1.67 1.50 and inherits the dimensions of t(NN): 0.50 2.00 0.67 1.67 0.75 1.50 which can be transposed back as in t( t( NN ) / lambda ): 0.50 0.67 0.75 2.00 1.67 1.50 but that requires a lot of moving elements around while sweep does not. Operators are not necessarily "better" than named functions... they just look different. On February 27, 2024 11:54:26 AM PST, Evan Cooch wrote: >So, trying to convert a very long, somewhat technical bit of lin alg >MATLAB code to R. Most of it working, but raninto a stumbling block that >is probaably simple enough for someone to explain. > >Basically, trying to 'line up' MATLAB results from an element-wise >division of a matrix by a vector with R output. > >Here is a simplified version of the MATLAB code I'm translating: > >NN = [1, 2, 3; 4, 5, 6]; % Example matrix >lambda = [2, 3, 4]; % Example vector >result_matlab = NN ./ lambda; > >which yields > > 0.5 0.7 0.75000 > 2.0 1.7 1.5 > > >So, the only way I have stumbled onto in R to generate the same results >is to use 'sweep'. The following 'works', but I'm hoping someone can >explain why I need something as convoluted as this seems (to me, at least). > >NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Example matrix >lambda <- c(2, 3, 4) # Example vector >sweep(NN, 2, lambda, "/") > > > [,1] [,2] [,3] >[1,] 0.5 0.667 0.75 >[2,] 2.0 1.667 1.50 > >First tried the more 'obvious' NN/lambda, but that yields 'the wrong >answer' (based solely on what I'm trying to accomplish): > > > [,1] [,2] [,3] >[1,] 0.50 0.5 1.0 >[2,] 1.33 2.5 1.5 > >So, why 'sweep'? > > [[alternative HTML version deleted]] > >__ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >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. -- Sent from my phone. Please excuse my brevity. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] converting MATLAB -> R | element-wise operation
So, trying to convert a very long, somewhat technical bit of lin alg MATLAB code to R. Most of it working, but raninto a stumbling block that is probaably simple enough for someone to explain. Basically, trying to 'line up' MATLAB results from an element-wise division of a matrix by a vector with R output. Here is a simplified version of the MATLAB code I'm translating: NN = [1, 2, 3; 4, 5, 6]; % Example matrix lambda = [2, 3, 4]; % Example vector result_matlab = NN ./ lambda; which yields 0.5 0.7 0.75000 2.0 1.7 1.5 So, the only way I have stumbled onto in R to generate the same results is to use 'sweep'. The following 'works', but I'm hoping someone can explain why I need something as convoluted as this seems (to me, at least). NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Example matrix lambda <- c(2, 3, 4) # Example vector sweep(NN, 2, lambda, "/") [,1] [,2] [,3] [1,] 0.5 0.667 0.75 [2,] 2.0 1.667 1.50 First tried the more 'obvious' NN/lambda, but that yields 'the wrong answer' (based solely on what I'm trying to accomplish): [,1] [,2] [,3] [1,] 0.50 0.5 1.0 [2,] 1.33 2.5 1.5 So, why 'sweep'? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.