矩陣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 –