Hi all, I have a set of patterns which can occur in a series of (3) matrices. I want to identify those and create a fourth one with the identifiers of the cases. Something like: for (i in 1:l) { for (j in 1:w) { A[A[i,j]==1 & D[i,j]==1 & P[i,j]==1] <- Case1; A[A[i,j]==-1 & D[i,j]==-1 & P[i,j]==-1] <- Case2; etc.... } } the code seems to run, but is very slow.... Could anyone please suggest a better approach? I was thinking that 3 matrices could be stacked in a cube, and the column of a cube searched for a pattern, but am not sure how to do that... Thanks Martin
Hi all, I have a set of patterns which can occur in a series of (3) matrices. I want to identify those and create a fourth one with the identifiers of the cases. Something like: for (i in 1:l) { for (j in 1:w) { A[A[i,j]==1 & D[i,j]==1 & P[i,j]==1] <- Case1; A[A[i,j]==-1 & D[i,j]==-1 & P[i,j]==-1] <- Case2; etc.... } } the code seems to run, but is very slow.... Could anyone please suggest a better approach? I was thinking that 3 matrices could be stacked in a cube, and the column of a cube searched for a pattern, but am not sure how to do that... Thanks Martin
You are putting your results back into "A" which might change things as you execute. This might be a faster way: result <- matrix(NA,dim(A)[1], dim(A)[2]) # now compute the cases result[(A ==1) & (D == 1) & (P ==1)] <- Case1 result[(A == -1) & (D == -1) & (P == -1)] <- Case2 ....... On Nov 8, 2007 12:27 PM, Martin Tomko <martin.tomko at gmail.com> wrote:> Hi all, > > I have a set of patterns which can occur in a series of (3) matrices. I > want to identify those and create a fourth one with the identifiers of > the cases. > > Something like: > > for (i in 1:l) { > for (j in 1:w) { > > A[A[i,j]==1 & D[i,j]==1 & P[i,j]==1] <- Case1; > A[A[i,j]==-1 & D[i,j]==-1 & P[i,j]==-1] <- Case2; > > etc.... > } > } > > the code seems to run, but is very slow.... Could anyone please suggest > a better approach? I was thinking that 3 matrices could be stacked in a > cube, and the column of a cube searched for a pattern, but am not sure > how to do that... > > Thanks > Martin > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Assume entries which are neither Case1 nor Case2 should be set to 0. Then: Case1 * (A == 1) * (D == 1) * (P == 1) + Case2 * (A == -1) * (D == -1) * (P == -1) # if A, D and P have their component values in the set [-1, 1] then this works too: Case1 * (pmin(A, D, P) == 1) + Case2 * (pmax(A, D, P) == -1) On Nov 8, 2007 12:27 PM, Martin Tomko <martin.tomko at gmail.com> wrote:> Hi all, > > I have a set of patterns which can occur in a series of (3) matrices. I > want to identify those and create a fourth one with the identifiers of > the cases. > > Something like: > > for (i in 1:l) { > for (j in 1:w) { > > A[A[i,j]==1 & D[i,j]==1 & P[i,j]==1] <- Case1; > A[A[i,j]==-1 & D[i,j]==-1 & P[i,j]==-1] <- Case2; > > etc.... > } > } > > the code seems to run, but is very slow.... Could anyone please suggest > a better approach? I was thinking that 3 matrices could be stacked in a > cube, and the column of a cube searched for a pattern, but am not sure > how to do that... > > Thanks > Martin > > ______________________________________________ > 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. >