Better way to extract all the rows from a Matrix A that contain an element of a matrix B

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 14:34:10

Here's a new approach to achieve the result that you actually wanted to achieve. I corrected 2 mistakes that you made and replaced all the for loops with bsxfun which is a very efficient function to do stuff like this. For Matlab R2016b or newer you can also implicit expansion instead of bsxfun.
My starts at you implementation of the sliding window. Instead of your for-loop, you can use

stdInds=bsxfun(@plus,1:step:(size(a,1)-overlap),(0:3).');
std_vals=std(a(sub2ind(size(a),stdInds,repmat(5,size(stdInds)))));

here. The bsxfun creates an array that holds the rows of your windows. It holds 1 windo in each column. These rows need to be transformed into linear index of the a-array in order to get an array of values, that can be passed to the std-function. In your implementation you made a small mistake here, because your for-loop ends at size(a,1)-window_size and should actually have ended at size(a,1)-overlap, because otherwise you are missing the last window.
Now that we got the std-values of the windows we can check which ones are greater than your predefined threshhold and then transform them back into the corresponding rows:

highStdWindows=find(std_vals_2>threshold);
highStdRows=bsxfun(@plus,highStdWindows*step-step+1,(0:3).');

highStdWindows contains the indexes of the windows, that have high-Std-values. In the next line, we calculate the starting rows of these windows using highStdWindows*step-step+1 and then we calculate the other rows that are corresponding to each window using the bsxfun again.
Now we get to the actual mistake in your code. This line right here

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];

does not do what you wanted it to do. Unfortunatly you missplaced a couple of brackets here. This way you take the large_windows(i)'th element of matrix a and substract 4*l_2_a_r_e from it. What you wanted to write was

B{i}==[a(large_windows(i)-l_2_a_r_e*4)  % and so on

This way you would substract the 4*l_2_a_r_e from the index that you pass to a. This would still be wrong, because in large_windows you stored row-numbers and not linear indexes corresponding to matrix a.
Nevertheless this can be achieved a lot easier using subscripted indexing instead of linear indexing:

rowList=reshape(highStdRows,1,[]);
C=a(rowList,:); % all columns (:) and from the rows in rowList

These two easy lines tell matlab to take all rows that are stored in highStdRows with all columns (expressed by the :). With this if there are two adjacent windows with high-Std-values you will get the overlapping rows twice. If you don't want that, you can use this code instead:

rowList=unique(reshape(highStdRows,1,[]));
C=a(rowList,:);

If you want to get further insides on how indexing in Matlab works take a look at LuisMendo's post about this topic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!