【发布时间】:2017-10-31 02:13:04
【问题描述】:
矩阵 A 是我的起始矩阵,它将 MPU6050 和 GPS 记录的数据保存在 SD 卡上(纬度、经度、时间、Ax、Ay、Az、Gx、Gy、Gz)。
我计算了窗口大小为 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 不包含在 A 中,所以这个问题没有帮助 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).'来创建large_windows数组,而不是您的for 循环。它可能会更有效一些。你在找这样的东西吗?你的目标是让你的代码更快吗?您在处理大量数据吗?或者您只是想美化代码并理解一些花哨的 matlab 索引内容? -
@uomodellamansarda 如果最终结果是矩阵 B,那么您实际上不需要计算矩阵“large_windows”。您可以直接从“large_indexes”中获取“B”。你怎么看?
-
@Max 我的目标是让我的代码更快,因为我有超过 4k 行,但我想了解一些花哨的 matlab 索引的东西(它们不是有用吗?我是菜鸟,没有计算机科学背景,每个人都不鼓励我在 matlab 上使用 for-loop):) 感谢您的建议,我会学习然后尝试 :)
-
@AmritbirSinghGill 我没有想到这个可能的解决方案,我会试试的! (做不做没有尝试)
-
在您计算
B的行中,您使用与a-array 的第 5 列对应的行号作为线性索引。你确定这是你想做的吗?
标签: matlab matrix indexing vectorization submatrix