【发布时间】:2014-02-10 09:41:28
【问题描述】:
我为异常值检测构建了一个函数,它工作得很好,但考虑到我正在处理的大量数据,我需要删除“for循环”,所以这里我们有矢量化版本(或者至少是什么我认为是我的代码的矢量化版本)。调用该函数,用户会初始化以下参数,我正在使用以下参数:
alpha=3
gamma=0.5
k=5
“价格”系列存在于工作区中,在调用函数时被链接。
我想我几乎做到了,但我遇到了一个问题。
这是一段代码:
[n] = size(price,1);
x = price;
[j1]=find(x); %output is a column vector with size (n,1) of the following form j1=[1:1:n]
matrix_left=zeros(n, k,'double');
matrix_right=zeros(n, k,'double');
toc
matrix_left(j1(k+1:end),:)=x(j1-k:j1-1);
%这里返回以下错误:下标索引必须是正整数或逻辑数。
matrix_right(j1(1:end-k),:)=x(j1+1:j1+k);
%这里说的是:Subscripted assignment dimension mismatch.
matrix_group=[matrix_left matrix_right];
trimmed_mean=trimmean(matrix_group,10,'round',2);
score=bsxfun(@minus,x,trimmed_mean);
sd=std(matrix_group,2);
temp = abs(score) > (alpha .* sd + gamma);
outmat = temp*1;
我想要的是这样的:
如果 k= 5
left_matrix (3443,5):
[100.25 103.5 102.25 102.75 103] <---5 left neighbouring observations of the 15th row of **x**
[103.5 102.25 102.75 103 103.5] <---5 left neighbouring observations of the 16th row of **x**
right_matrix(3443,5):
[103.75 104.25 104 104.75 104.25] <---5 right neighbouring observations of the 15th row of **x**
[104.25 104 104.75 104.25 104.5] <---5 right neighbouring observations of the 16th row of **x**
以下是一小部分数据:
x = Price; price size = (3443, 1)
[...]
100.25 %// '*suppose here we are at the 10th row*'
103.5
102.25
102.75
103
103.5 %// '*here we are at the 15th row*'
103.75
104.25
104
104.75
104.25
104.5
[...]
Time (3443,1) %// the same as price, it reports the time of the transaction (HH:MM:SS).
j1 (3443,1)
1
2
[...]
3442
3443
提前感谢大家,
乔治
【问题讨论】:
-
只要
j1小于或等于k,或小于等于1,就会出现该错误 -
@Dan 你指的是matrix_left吗?但是以我编写代码的方式,它不应该被限制在 j1(k+1:end) 上。特别是我选择了k=5。如果我理解正确,如果 k=1,我应该有那个错误。
-
我指的是
...=x(j1-k:j1-1)。如果k是5和j1原来是5或更少,那么你会得到这个错误 -
@Dan 我这样定义 j1:[j1]=find(x);因此,作为输出,我有一个来自 (1:n) 的简单整数列向量。我明白你的意思,但我认为我正确地限制了 matrix_left 的索引,以便仅从 j1>k 开始填充它。
-
如果没有看到
j1(或者price),就不可能确切地说出发生了什么,但你的问题是你如何在该行中索引x,而不是matrix_left。错误来自等式的右侧
标签: matlab function matrix vectorization