I'm trying to write a function that will generate a NxN matrix that has the value K on both diagonals, while the values outside the diagonals (up and down) are 1's (for conflicting positions such as [4,5] and [5,4] the larger value is written in the matrix). Basically, I'm trying to replicate this matrix (where N = 8 and K = 5): 5 1 0 0 0 0 1 5 1 5 1 0 0 1 5 1 0 1 5 1 1 5 1 0 0 0 1 5 5 1 0 0 0 0 1 5 5 1 0 0 0 1 5 1 1 5 1 0 1 5 1 0 0 1 5 1 5 1 0 0 0 0 1 5 Any ideas? Thank you! Srdjan Santic, M.Sc. Graduate Student Faclty of Economics University of Belgrade Serbia --- This email is free from viruses and malware because avast! Antivirus protection is active.
HI, You could try: ?mat1 <- matrix(0,8,8) ?diag(mat1) <- 5 ?mat2 <- apply(mat1,2,rev) ?diag(mat2) <- 5 ?indx<- which(mat2==5) ?mat2[indx[indx%%8==1]+1] <-1 mat2[indx[indx%%8==0]-1] <-1 ?mat2[indx[!(indx%%8==0 |? indx%%8==1)]-1][mat2[indx[!(indx%%8==0 |indx%%8==1)]-1]==0] <- 1 ?mat2[indx[!(indx%%8==0 |? indx%%8==1)]+1][mat2[indx[!(indx%%8==0 |indx%%8==1)]+1]==0] <- 1 A.K. On Tuesday, December 3, 2013 10:07 AM, Srdjan Santic <srdjan.santic at gmail.com> wrote: I'm trying to write a function that will generate a NxN matrix that has the value K on both diagonals, while the values outside the diagonals (up and down) are 1's (for conflicting positions such as [4,5] and [5,4] the larger value is written in the matrix). Basically, I'm trying to replicate this matrix (where N = 8 and K = 5): 5? 1? 0? 0? 0? 0? 1? 5 1? 5? 1? 0? 0? 1? 5? 1 0? 1? 5? 1? 1? 5? 1? 0 0? 0? 1? 5? 5? 1? 0? 0 0? 0? 1? 5? 5? 1? 0? 0 0? 1? 5? 1? 1? 5? 1? 0 1? 5? 1? 0? 0? 1? 5? 1 5? 1? 0? 0? 0? 0? 1? 5 Any ideas? Thank you! Srdjan Santic, M.Sc. Graduate Student Faclty of Economics University of Belgrade Serbia --- This email is free from viruses and malware because avast! Antivirus protection is active. ______________________________________________ R-help at r-project.org 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.
We don't do homework assignments on the list, but one answer requires no loops and an understanding of the various ways R uses vectors for indexing: ?"[" ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Srdjan Santic Sent: Tuesday, December 3, 2013 5:44 AM To: r-help at r-project.org Subject: [R] Generating a matrix I'm trying to write a function that will generate a NxN matrix that has the value K on both diagonals, while the values outside the diagonals (up and down) are 1's (for conflicting positions such as [4,5] and [5,4] the larger value is written in the matrix). Basically, I'm trying to replicate this matrix (where N = 8 and K = 5): 5 1 0 0 0 0 1 5 1 5 1 0 0 1 5 1 0 1 5 1 1 5 1 0 0 0 1 5 5 1 0 0 0 0 1 5 5 1 0 0 0 1 5 1 1 5 1 0 1 5 1 0 0 1 5 1 5 1 0 0 0 0 1 5 Any ideas? Thank you! Srdjan Santic, M.Sc. Graduate Student Faclty of Economics University of Belgrade Serbia --- This email is free from viruses and malware because avast! Antivirus protection is active. ______________________________________________ R-help at r-project.org 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.