矩阵A是我的起始矩阵,它包含从SD卡上的MPU6050和GPS记录的数据(经度,纬度,时间,斧,Ay,Az,Gx,Gy,Gz)。从矩阵A中提取所有行的更好方法,该矩阵包含矩阵B的一个元素
我计算了窗口大小为5的Az的标准偏差,并确定了满足条件(>阈值)的所有元素。
然后在矩阵“large_windows”我存储窗口中满足条件的所有Az的索引。
从矩阵“large_windows”我计算新的矩阵B与所有从矩阵A包含矩阵的行“large_windows”元件。
我觉得我的代码是effective,但非常丑陋和混乱,加上我仍然不是很实用与indexing,但我想了解它。
1.是否有更好的解决方案?
2.可以使用一个逻辑索引?怎么样?它是有效的*?
这里我的代码,是一个简单的例子,与一般条件,了解整个概念更好的不仅是我的具体情况, starting from suggestions of a previous problem(how to create a sliding window
%random matix nXm
a=rand(100,6);
%window dimension
window_size=4;
%overlap between two windows
overlap=1;
%increment needed
step=window_size - overlap;
%std threshold
threshold=0.3;
std_vals= NaN(size(a,1),1);
%The sliding window will analyze only the 5th column
for i=1: step: (size(a,1)-window_size)
std_vals(i)=std(a(i:(i+window_size-1),5));
end
% finding the rows with standard deviation larger than threshold
large_indexes = find(std_vals>threshold);
%Storing all the elements that are inside the window with std>threshold
large_windows = zeros(numel(large_indexes), window_size);
for i=1:window_size
large_windows(:,i) = large_indexes + i - 1;
end
% Starting extracting all the rows with the 5th column outlier elements
n=numel(large_windows);
%Since i will work can't know how long will be my dataset
%i need to knwo how is the "index distance" between two adjacent elements
% in the same row [es. a(1,1) and a(1,2)]
diff1=sub2ind(size(a),1,1);
diff2=sub2ind(size(a),1,2);
l_2_a_r_e = diff2-diff1 %length two adjacent row elements
large_windows=large_windows'
%calculating al the index of the element of a ith row containing an anomaly
for i=1:n
B{i}=[a(large_windows(i))-l_2_a_r_e*4 a(large_windows(i))-l_2_a_r_e*3 a(large_windows(i))-l_2_a_r_e*2 a(large_windows(i))-l_2_a_r_e*1 a(large_windows(i))-l_2_a_r_e*0 a(large_windows(i))+l_2_a_r_e];
end
C= cell2mat(B');
我还发布之前读了一些问题,但This was to specific
B的不包括在所以这个问题是不是在THI有益Find complement of a data frame (anti - join)
I don't know how to useismember
的具体情况
我希望我的画能更好地解释我的问题:)
我不知道我是否正确理解你在找什么。例如,您可以使用'large_windows = repmat(large_indexes。',window_size,1)+(0:3)。'或'large_windows = bsxfun(@ plus,large_indexes,0:3)来代替for循环。 '创建你的'large_windows'数组。这可能会更有效一些。你在找这种东西吗?你的目标是让你的代码更快吗?你在处理大量的数据吗?或者你只是试图预先验证代码并理解一些花哨的matlab索引的东西? – Max
@uomodellamansarda如果最终结果是矩阵B,那么您并不需要计算矩阵“large_windows”。您可以直接从“large_indexes”获得“B”。 您对此有何看法? –
@Max我的目标是让我的代码更快,因为我有超过4k行,但我想了解一些花哨的matlab索引的东西(他们没有用吗?我是noob,没有计算机科学背景,每个人都让我感到沮丧在MATLAB上使用for-loop):)感谢您的建议,我将学习,然后尝试它:) <3 –