neuroexplorer 发表于 2015-12-8 01:25 
also, you do not have to use loop here. You can use logic indexing, instead. The latter is a lot fas ...
Just start using R, so most of my questions maybe stupid in your opinion.
I'm sure your solution is much better than mine, but I can't quite understand it so far.
I'll try to make it.
Here is another question:
Now I got a 10*13 matrix, every row represents a time series (T=0-12) of the price of a stock, once stock price>=110 for 3 consecutive time steps, print that T, otherwise print 0 at the end of this time period.
And here is the code I tried. Although it's obviously wrong:
Tseries<-c(1:10)
T=0
for(i in 1:10){
for(j in 1:13){
if(S[i,j]>=110){T<-T+1
}else{
T<-0
}Tseries[j]<-T
}
}
What should I do to solve this question?
Thank you a lot.