Re: [R] [External] converting MATLAB -> R | element-wise operation

2024-02-29 Thread Evan Cooch
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. >

Re: [R] [External] converting MATLAB -> R | element-wise operation

2024-02-29 Thread Richard M. Heiberger
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, "/"),

Re: [R] [External] converting MATLAB -> R | element-wise operation

2024-02-29 Thread Richard M. Heiberger
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 =

Re: [R] [External] converting MATLAB -> R | element-wise operation

2024-02-28 Thread Evan Cooch
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'

Re: [R] [External] converting MATLAB -> R | element-wise operation

2024-02-28 Thread Berwin A Turlach
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,

Re: [R] [External] converting MATLAB -> R | element-wise operation

2024-02-27 Thread Richard M. Heiberger
> 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.