Re: [R] [External] converting MATLAB -> R | element-wise operation
Very interesting - thanks! Most of my problems are not limited by compute speed, but its clear that for some sorts of compute-intensive problems, sweep might be a limiting approach. On 2/29/2024 6:12 PM, Richard M. Heiberger wrote: > I decided to do a direct comparison of transpose and sweep. > > > library(microbenchmark) > > NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Example matrix > lambda <- c(2, 3, 4) # Example vector > colNN <- t(NN) > > microbenchmark( >sweep = sweep(NN, 2, lambda, "/"), >transpose = t(t(NN)/lambda), >colNN = colNN/lambda > ) > > > Unit: nanoseconds >expr minlq mean median uq max neval cld > sweep 13817 14145 15115.06 14350 14657.5 75932 100 a > transpose 1845 1927 2151.68 2132 2214.0 7093 100 b > colNN82 123 141.86123 164.0 492 100 c > > Note that transpose is much faster than sweep because it is doing less work, > I believe essentially just changing the order of indexing. > > Using the natural sequencing for column-ordered matrices is much much faster. > >> On Feb 28, 2024, at 18:43, peter dalgaard wrote: >> >>> rbind(1:3,4:6)/t(matrix(c(2,3,4), 3,2)) > [[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] [External] converting MATLAB -> R | element-wise operation
I added two more rows library(microbenchmark) NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Example matrix lambda <- c(2, 3, 4) # Example vector colNN <- t(NN) matlam <- matrix(lambda, byrow=TRUE, nrow=2, ncol=3) microbenchmark( sweep = sweep(NN, 2, lambda, "/"), transpose = t(t(NN)/lambda), colNN = colNN/lambda, fullsize = NN / matrix(lambda, byrow=TRUE, nrow=2, ncol=3), rowlam = NN / matlam ) Unit: nanoseconds expr minlq mean median uq max neval cld sweep 12546 12792 13919.91 12997.0 13325.0 85608 100 a transpose 1640 1763 1986.04 1947.5 2050.0 7462 100 b colNN8282 161.13 123.0 123.0 3854 100 c fullsize 738 820 932.34 881.5 963.5 2829 100 bc rowlam82 123 168.92 164.0 164.0 820 100 c reshaping the denominator to the correct size in advance is very helpful if you will be doing this division more than once. > On Feb 29, 2024, at 18:12, Richard M. Heiberger wrote: > > I decided to do a direct comparison of transpose and sweep. > > > library(microbenchmark) > > NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Example matrix > lambda <- c(2, 3, 4) # Example vector > colNN <- t(NN) > > microbenchmark( > sweep = sweep(NN, 2, lambda, "/"), > transpose = t(t(NN)/lambda), > colNN = colNN/lambda > ) > > > Unit: nanoseconds > expr minlq mean median uq max neval cld > sweep 13817 14145 15115.06 14350 14657.5 75932 100 a > transpose 1845 1927 2151.68 2132 2214.0 7093 100 b > colNN82 123 141.86123 164.0 492 100 c > > Note that transpose is much faster than sweep because it is doing less work, > I believe essentially just changing the order of indexing. > > Using the natural sequencing for column-ordered matrices is much much faster. > >> On Feb 28, 2024, at 18:43, peter dalgaard wrote: >> >>> rbind(1:3,4:6)/t(matrix(c(2,3,4), 3,2)) > > __ > 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] [External] converting MATLAB -> R | element-wise operation
I decided to do a direct comparison of transpose and sweep. library(microbenchmark) NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Example matrix lambda <- c(2, 3, 4) # Example vector colNN <- t(NN) microbenchmark( sweep = sweep(NN, 2, lambda, "/"), transpose = t(t(NN)/lambda), colNN = colNN/lambda ) Unit: nanoseconds expr minlq mean median uq max neval cld sweep 13817 14145 15115.06 14350 14657.5 75932 100 a transpose 1845 1927 2151.68 2132 2214.0 7093 100 b colNN82 123 141.86123 164.0 492 100 c Note that transpose is much faster than sweep because it is doing less work, I believe essentially just changing the order of indexing. Using the natural sequencing for column-ordered matrices is much much faster. > On Feb 28, 2024, at 18:43, peter dalgaard wrote: > >> rbind(1:3,4:6)/t(matrix(c(2,3,4), 3,2)) __ 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] [External] converting MATLAB -> R | element-wise operation
Many thanks for the collective answers -- consider this a thank you to the group. I had 'guessed' it had something to do with 'columns then rows' or vice versa (MATLAB convention vs R convention), but had never heard about 'sweep' before. Most of the time when I run into 'matrix orientation' issues, I simply transpose as needed, but that can get clunky. 'sweep' has some utility I'll tuck away if needed in future. Cheers - and thanks again. On 2/27/2024 4:37 PM, Richard M. Heiberger wrote: >> t(t(NN)/lambda) > [,1] [,2] [,3] > [1,] 0.5 0.667 0.75 > [2,] 2.0 1.667 1.50 > R matrices are column-based. MATLAB matrices are row-based. > >> On Feb 27, 2024, at 14:54, 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 guidehttp://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] [External] converting MATLAB -> R | element-wise operation
On Tue, 27 Feb 2024 21:37:52 + "Richard M. Heiberger" wrote: > > t(t(NN)/lambda) > [,1] [,2] [,3] > [1,] 0.5 0.667 0.75 > [2,] 2.0 1.667 1.50 > > > > R matrices are column-based. MATLAB matrices are row-based. It might depend on what you mean with this statement, but I would be very surprised if MATLAB is not storing matrices in column-major form, just as R does. NN = [1, 2, 3; 4, 5, 6]; and NN = [ [1, 4]' , [2, 5]', [3, 6]' ]; produce the same matrix in MATLAB. So the default filling of matrices is by row, and there is no convenient argument to change that. 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] [External] converting MATLAB -> R | element-wise operation
> t(t(NN)/lambda) [,1] [,2] [,3] [1,] 0.5 0.667 0.75 [2,] 2.0 1.667 1.50 > R matrices are column-based. MATLAB matrices are row-based. > On Feb 27, 2024, at 14:54, 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. __ 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.