g**r 发帖数: 425 | 1 My data looks like this:
0 0 0 1 0
0 1 0 0 0
0 0 1 0 0
...
Say each columns stands for a time, so I have 5 time points. I want to
select all the combination that the '1's happen on or after the current
matrix. So for 1st row, possible value would be 1 at time 4 or 5; for
second row, would be 2,3,4,5 and third row, 3, 4, 5.
Or, sth like the following code:
kf=c(0,1,0,0,0,1,1,0,0,0,0,0)
kf=matrix(kf,3)
for(i in 3:4)
for (j in 1:4)
for (k in 3:4){
kf[1 | D******n 发帖数: 2836 | 2 This is definitely worth some baozis...
kf=c(0,1,0,0,0,1,1,0,0,0,0,0)
kf=matrix(kf,3)
nrow <- nrow(kf);
ncol <- ncol(kf);
oripos <- which(t(kf)==1,arr.ind=T)[,1];
nowpos <- oripos;
rowptr <- 1;
cat(nowpos,'\n');
while (rowptr<=nrow)
{
if ( nowpos[rowptr]
{ nowpos[rowptr] = nowpos[rowptr]+1;
rowptr = 1;
cat(nowpos,'\n');
}
else {
nowpos[rowptr] = oripos[rowptr];
rowptr = rowptr + 1;
}
}
【在 g**r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : My data looks like this: : 0 0 0 1 0 : 0 1 0 0 0 : 0 0 1 0 0 : ... : Say each columns stands for a time, so I have 5 time points. I want to : select all the combination that the '1's happen on or after the current : matrix. So for 1st row, possible value would be 1 at time 4 or 5; for : second row, would be 2,3,4,5 and third row, 3, 4, 5. : Or, sth like the following code:
| q**j 发帖数: 10612 | 3 alternatively:
> y = t(apply(kf,1,cumsum))
> y
[,1] [,2] [,3] [,4]
[1,] 0 0 1 1
[2,] 1 1 1 1
[3,] 0 1 1 1
> x = rep(1:dim(kf)[2],dim(kf)[1])
> x
[1] 1 2 3 4 1 2 3 4 1 2 3 4
> x = matrix(x,byrow=TRUE,dim(kf)[1])
> x
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 2 3 4
[3,] 1 2 3 4
> x*y
[,1] [,2] [,3] [,4]
[1,] 0 0 3 4
[2,] 1 2 3 4
[3,] 0 2 3 4
【在 D******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : This is definitely worth some baozis... : kf=c(0,1,0,0,0,1,1,0,0,0,0,0) : kf=matrix(kf,3) : nrow <- nrow(kf); : ncol <- ncol(kf); : oripos <- which(t(kf)==1,arr.ind=T)[,1]; : nowpos <- oripos; : rowptr <- 1; : cat(nowpos,'\n'); : while (rowptr<=nrow)
| g**r 发帖数: 425 | 4 Thanks, this works! Check your bank:)
【在 D******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : This is definitely worth some baozis... : kf=c(0,1,0,0,0,1,1,0,0,0,0,0) : kf=matrix(kf,3) : nrow <- nrow(kf); : ncol <- ncol(kf); : oripos <- which(t(kf)==1,arr.ind=T)[,1]; : nowpos <- oripos; : rowptr <- 1; : cat(nowpos,'\n'); : while (rowptr<=nrow)
| g**r 发帖数: 425 | 5 Almost.
After adding k.list=list(kf)
for(i in 1:nrow(kf))k.list[[i]]=subset(k[i,],k[i,]!=0)
expand.grid(k.list)
Get the same results.
Thanks!
【在 q**j 的大作中提到】![](/moin_static193/solenoid/img/up.png) : alternatively: : > y = t(apply(kf,1,cumsum)) : > y : [,1] [,2] [,3] [,4] : [1,] 0 0 1 1 : [2,] 1 1 1 1 : [3,] 0 1 1 1 : > x = rep(1:dim(kf)[2],dim(kf)[1]) : > x : [1] 1 2 3 4 1 2 3 4 1 2 3 4
|
|