本文介绍了Matlab:使用不含for循环的列向量中的移动间隔填充矩阵行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我为离群值检测构建了一个函数,它工作得很好,但考虑到我正在处理大量的数据,我需要删除for循环,所以在这里我们有矢量化版本(或者至少是什么我认为是我的代码的矢量化版本)。调用函数以下参数由用户初始化,我正在处理以下内容: alpha = 3 gamma = 0.5 k = 5 工作区中存在系列价格当调用该函数时。 我认为我几乎做到了,但是我遇到了一个问题。 这是一段代码: [n] = size(price,1); x =价格; [j1] = find(x); %输出是具有以下形式的大小(n,1)的列向量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); %这里它返回以下错误:下标索引必须是真正的正整数或逻辑。 (j1(1:end-k),:)= x(j1 + 1:j1 + k);其中, %这里,它表示如下:下标赋值尺寸不匹配。 matrix_group = [matrix_left matrix_right]; trimmed_mean = trimmean(matrix_group,10,'round',2); 分数= bsxfun(@ minus,x,trimmed_mean); sd = std(matrix_group,2); temp = abs(分数)> (alpha。* sd + gamma); outmat = temp * 1; 我想要的是类似: 如果k = 5 left_matrix(3443,5): [100.25 103.5 102.25 102.75 103] [103.5 102.25 102.75 103 103.5] right_matrix(3443,5): [103.75 104.25 104 104.75 104.25]< --- 5 ** x ** 的第15行的右边相邻观察值[104.25 104 104.75 104.25 104.5] < --- 5 ** ** $ b的第16行的右边相邻观察值 $ b 以下是一小部分数据: x = Price;价格大小=(3443,1) [...] 100.25%//'*假设我们在第10行*' 103.5 102.25 102.75 103 103.5%//'*我们在第15行*' 103.75 104.25 104 104.75 104.25 104.5 时间(3443,1)%//与价格相同,它报告交易时间(HH:MM:SS )。 j1(3443,1) 1 2 [...] 3442 3443 预先感谢大家, Giorgio 解决方案 $ b [n] =尺寸(价格,1); x =价格; idxArray_left = bsxfun(@plus,(k + 1:n)', - k:-1); idxArray_fill_left = bsxfun(@plus,(1:k)',1:k); matrix_left = [idxArray_fill_left; idxArray_left]; idxArray_right = bsxfun(@plus,(1:n-k)',1:k); idxArray_fill_right = bsxfun(@plus,(n-k + 1:n)', - k:-1); matrix_right = [idxArray_right; idxArray_fill_right]; idx_matrix = [matrix_left matrix_right]; neigh_matrix = x(idx_matrix); trimmed_mean = trimmean(neigh_matrix,10,'round',2); 分数= bsxfun(@ minus,x,trimmed_mean); sd = std(neigh_matrix,0,2); temp = abs(分数)> (alpha。* sd + gamma); outmat = temp * 1; 感谢Jonas给了我一个很好的提示来解决这个问题I built a function for outliers detection and it worked quite well, but given the huge amount of data I'm working on I needed to remove the "for loop", so here we have the vectorized version (or at least what I think is a vectorized version of my code). Calling the function the following parameters are intialized by the user, I am working with the following: alpha=3gamma=0.5k=5The series "price" exist in the workspace, is linked when calling the function. I think I almost did it but I am stuck with a problem.Here is a piece of the code: [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');tocmatrix_left(j1(k+1:end),:)=x(j1-k:j1-1);%Here it returns the following error: Subscript indices must either be real positive integers or logicals.matrix_right(j1(1:end-k),:)=x(j1+1:j1+k);%Here, it says the following: 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; What I'd like to have is something like:if 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**Here is a small sample of data:x = Price; price size = (3443, 1)[...] 100.25 %// '*suppose here we are at the 10th row*' 103.5102.25102.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 Thanks everyone in advance,Giorgio 解决方案 Here is the answer, the way to go was (once again) bsxfun: [n] = size(price,1);x = price;idxArray_left=bsxfun(@plus,(k+1:n)',-k:-1);idxArray_fill_left=bsxfun(@plus,(1:k)',1:k);matrix_left=[idxArray_fill_left; idxArray_left];idxArray_right=bsxfun(@plus,(1:n-k)',1:k);idxArray_fill_right=bsxfun(@plus,(n-k+1:n)',-k:-1);matrix_right=[idxArray_right; idxArray_fill_right]; idx_matrix=[matrix_left matrix_right];neigh_matrix=x(idx_matrix);trimmed_mean=trimmean(neigh_matrix,10,'round',2);score=bsxfun(@minus,x,trimmed_mean);sd=std(neigh_matrix,0,2);temp = abs(score) > (alpha .* sd + gamma);outmat = temp*1; Thanks to Jonas that gave me a great hint to solve the problem 这篇关于Matlab:使用不含for循环的列向量中的移动间隔填充矩阵行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 18:15