Hi my R friends,
I have two matrices as follows: M<-matrix(c(1,4,1,3,1,4,2,3,1,2,1,2),3) 1 3 2 2 4 1 3 1 1 4 1 2 N<-matrix(c(1,1,2,2,3,4,-2,2,1,4,3,-1),3) 1 2 -2 4 1 3 2 3 2 4 1 -1 I want to find a vector which is a matrix 1*3 and each of its elements is the multiplication of min element of each row of M by the max element of the corresponding row of N (for example, the first element of the vector is the min element of the first row of matrix M, which is 1, multiply by the max element of the first row of matrix N, which is 4, and so the first element of the vector is 1*4 which is 4). The final answer is: (1*4, 1*3,1*4)=(4,3,4) To find this vector (or matrix) I have written the below code: c(min(M[1,])*max(N[1,]),min(M[2,])*max(N[2,]),min(M[3,])*max(N[3,])) But it is so long. could anyone writes a shorter (or simpler, or easier) code? [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
Hello,
Use ?apply on each of the matrices. min_max <- function(X, Y, na.rm = FALSE){ Min <- apply(X, 1, min, na.rm = na.rm) Max <- apply(Y, 1, max, na.rm = na.rm) Min*Max } min_max(M, N) #[1] 4 3 4 Hope this helps, Rui Barradas Às 15:30 de 23/05/20, Vahid Borji escreveu: > Hi my R friends, > > I have two matrices as follows: > > M<-matrix(c(1,4,1,3,1,4,2,3,1,2,1,2),3) > > 1 3 2 2 > 4 1 3 1 > 1 4 1 2 > > N<-matrix(c(1,1,2,2,3,4,-2,2,1,4,3,-1),3) > > 1 2 -2 4 > 1 3 2 3 > 2 4 1 -1 > > I want to find a vector which is a matrix 1*3 and each of its elements is > the multiplication of min element of each row of M by the max element of > the corresponding row of N (for example, the first element of the vector is > the min element of the first row of matrix M, which is 1, multiply by the > max element of the first row of matrix N, which is 4, and so the first > element of the vector is 1*4 which is 4). > The final answer is: (1*4, 1*3,1*4)=(4,3,4) > > To find this vector (or matrix) I have written the below code: > c(min(M[1,])*max(N[1,]),min(M[2,])*max(N[2,]),min(M[3,])*max(N[3,])) > But it is so long. could anyone writes a shorter (or simpler, or easier) > code? > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] 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. |
In reply to this post by Vahid
Please post using plain text format... otherwise what we see can be corrupted as compared to what you saw.
The obvious solution is: apply( M, 1, min ) * apply( N, 1, max ) but Google sez there is an optimized version useful with large matrices: library( Rfast ) rowMins( M ) * rowMins( N ) (untested) On May 23, 2020 7:30:48 AM PDT, Vahid Borji <[hidden email]> wrote: >Hi my R friends, > >I have two matrices as follows: > >M<-matrix(c(1,4,1,3,1,4,2,3,1,2,1,2),3) > >1 3 2 2 >4 1 3 1 >1 4 1 2 > >N<-matrix(c(1,1,2,2,3,4,-2,2,1,4,3,-1),3) > >1 2 -2 4 >1 3 2 3 >2 4 1 -1 > >I want to find a vector which is a matrix 1*3 and each of its elements >is >the multiplication of min element of each row of M by the max element >of >the corresponding row of N (for example, the first element of the >vector is >the min element of the first row of matrix M, which is 1, multiply by >the >max element of the first row of matrix N, which is 4, and so the first >element of the vector is 1*4 which is 4). >The final answer is: (1*4, 1*3,1*4)=(4,3,4) > >To find this vector (or matrix) I have written the below code: >c(min(M[1,])*max(N[1,]),min(M[2,])*max(N[2,]),min(M[3,])*max(N[3,])) >But it is so long. could anyone writes a shorter (or simpler, or >easier) >code? > > [[alternative HTML version deleted]] > >______________________________________________ >[hidden email] 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. ______________________________________________ [hidden email] 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. |
In reply to this post by Rui Barradas
Hello,
I have just found out that you are cross posting [1]. Please do not cross post, ask a question, wait for an answer and after or 3 days, if you don't have a (satisfactory) answer, ask somewhere else. [1] https://stackoverflow.com/questions/61973737/min-rows-multiplication-by-the-max-rows Rui Barradas Às 15:52 de 23/05/20, Rui Barradas escreveu: > Hello, > > Use ?apply on each of the matrices. > > > min_max <- function(X, Y, na.rm = FALSE){ > Min <- apply(X, 1, min, na.rm = na.rm) > Max <- apply(Y, 1, max, na.rm = na.rm) > Min*Max > } > min_max(M, N) > #[1] 4 3 4 > > > Hope this helps, > > Rui Barradas > > Às 15:30 de 23/05/20, Vahid Borji escreveu: >> Hi my R friends, >> >> I have two matrices as follows: >> >> M<-matrix(c(1,4,1,3,1,4,2,3,1,2,1,2),3) >> >> 1 3 2 2 >> 4 1 3 1 >> 1 4 1 2 >> >> N<-matrix(c(1,1,2,2,3,4,-2,2,1,4,3,-1),3) >> >> 1 2 -2 4 >> 1 3 2 3 >> 2 4 1 -1 >> >> I want to find a vector which is a matrix 1*3 and each of its elements is >> the multiplication of min element of each row of M by the max element of >> the corresponding row of N (for example, the first element of the >> vector is >> the min element of the first row of matrix M, which is 1, multiply by the >> max element of the first row of matrix N, which is 4, and so the first >> element of the vector is 1*4 which is 4). >> The final answer is: (1*4, 1*3,1*4)=(4,3,4) >> >> To find this vector (or matrix) I have written the below code: >> c(min(M[1,])*max(N[1,]),min(M[2,])*max(N[2,]),min(M[3,])*max(N[3,])) >> But it is so long. could anyone writes a shorter (or simpler, or easier) >> code? >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> [hidden email] 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. >> > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
Free forum by Nabble | Edit this page |