我有一个429x1矢量,表示水文时间序列。我期待“滞后”时间序列的时间步长,并将其转化为一个矩阵输入到nftool进行一些人工神经网络分析。矩阵的宽度由输入层中的输入神经元数量控制,这是我从扩展表中读取的值。这就是我想用一个更短的时间序列来说明这个例子:
inp_neur = 5; % amount of input neurons (read in from excel)
A = [9;6;8;3;2]; % hypothetical hydrological time series
% do pad zero process
结果:
newA =
9 0 0 0 0
6 9 0 0 0
8 6 9 0 0
3 8 6 9 0
2 3 8 6 9
我相信这不是最难做的事,但能在一条船上完成吗?
任何帮助都将不胜感激。
干杯,
JQ
Another example with inp_neur = 7;
A = [11;35;63;21;45;26;29;84;51]
newA =
11 0 0 0 0 0 0
35 11 0 0 0 0 0
63 35 11 0 0 0 0
21 63 35 11 0 0 0
45 21 63 35 11 0 0
26 45 21 63 35 11 0
29 26 45 21 63 35 11
84 29 26 45 21 63 35
51 84 29 26 45 21 63
发布于 2012-12-19 04:35:20
我知道这个问题已被标记为已被接受,但我认为值得指出的是,如果(时间序列中的观测数)远远大于K
(即OP符号中的滞后数,即inp_neur
),那么当前接受的答案将是非常低效率的。这是因为它根据T
矩阵创建了一个T
,然后根据K
将其截断为T
。
我会提出两种可能的选择。第一种方法使用了一个来自计量经济学工具箱的函数,该工具箱的目的正是为了实现OP想要的结果:lagmatrix
。第二种是基于循环的解决方案。
lagmatrix
解决方案在OP想要0
的地方返回NaN
,因此需要增加一行来转换它们。全面的解决办法是:
newA2 = lagmatrix(A, 0:K-1);
newA2(isnan(newA2)) = 0;
基于循环的解决方案是:
newA3 = zeros(T, K);
for k = 1:K
newA3(k:end, k) = A(1:end-k+1);
end
基于循环的解决方案的明显优点是它不需要计量经济学工具箱。但这是唯一的优势吗?让我们试试计时跑。设置T = K = 10
。然后:
Elapsed time is 0.045809 seconds. %# 3lectrologos solution
Elapsed time is 0.049845 seconds. %# lagmatrix solution
Elapsed time is 0.017340 seconds. %# loop solution
3 lagmatrix
解和L solution解本质上是一致的。基于循环的解决方案要快3倍!现在,为了强调3 3lectrologos解决方案的问题,设置T = 1000
和K = 10
。然后:
Elapsed time is 10.615298 seconds.
Elapsed time is 0.149164 seconds.
Elapsed time is 0.056074 seconds.
现在3lectrologos解比lagmatrix
解慢两个数量级。但是今天真正的赢家是基于循环的解决方案,它的速度仍然是lagmatrix
解决方案的3倍。
结论:不再对Matlab中的单圈进行折扣.他们越来越快了!
对于那些感兴趣的人,计时运行的代码如下:
M = 1000; %# Number of iterations for timed test
T = 1000; %# Length of your vector of inputs
K = 10; %# Equivalent to your inp_neur
A = randi(20, T, 1); %# Generate random data
%# 3lectrologos solution (inefficient if T is large relative to K)
tic
for m = 1:M
tmp = tril(toeplitz(A));
newA1 = tmp(:, 1:K);
end
toc
%# lagmatrix solution
tic
for m = 1:M
newA2 = lagmatrix(A, 0:K-1);
newA2(isnan(newA2)) = 0;
end
toc
%# Loop based solution
tic
for m = 1:M
newA3 = zeros(T, K);
for k = 1:K
newA3(k:end, k) = A(1:end-k+1);
end
end
toc
发布于 2012-12-19 01:00:07
这里有一条两条船:
tmp = tril(toeplitz(A));
newA = tmp(:, 1:inp_neur);
https://stackoverflow.com/questions/13943964
复制相似问题