Reputation: 379
I used the same code for two different input matrices, In both cases i will call it "the input matrix A"
The first case is a 7000X4
The second case is a 29500X12
I need to split a selected column in windows
and then for each window
i need to calculate the std and store the value inside a std_vals
matrix
I took care to change the value of my input variables.
In the first case my goal was to analyze the 4th column, in the second case my goal was to analyze the 12th column
In the first case the code worked
In the second case the code returned me an error message
I tried to analyze all the steps in the code, but i dind't find the error.
Could you please help me to understand better?
column_length=size(A,1);
Amod= mod(column_length,100);
if Amod~=0
A=A(1:(size(A,1)-Amod),:);
end
newlenght=size(A);
%selected column vector to analyze
columnselected=4;
%window dimension
window_size=200;
%overlap between two windows
overlap=0;
%increment needed
step=window_size - overlap;
%std threshold
soglia=2;
std_vals= NaN(size(A,1),1);
devstd=std(A(:,4));
stdInds=bsxfun(@plus,1:step:(size(A,1)-overlap),(0:(window_size-1)).');
%In the first case size(stdInds)=200X35
%In the second case size(stdInds)=200X148
%In the first case size(repmat(columnselected,size(stdInds))= 200X35
%In the second case size(repmat(columnselected,size(stdInds))= 200X148
%In the first case size(A)=7000X4
%In the second case size(A)=29500X12
std_vals=std(A(sub2ind(size(A),stdInds,repmat(columnselected,size(stdInds)))));
highStdWindows=find(std_vals>soglia);
I am self taught. To understand better my error first i rewrite on a piece of paper the code, then i analyzed every step, and studied the output. I took me more than 4 hours.
I also tried to put in the second case a 7000X12 vector, but the code returned me the same error.
I think my method is not very efficent.
Upvotes: 2
Views: 1590
Reputation: 1511
The error you're getting from sub2ind
isn't caused by the size of the arguments, but rather by their values - so the best way to debug it would be to look at those arguments e.g. by plotting them.
Here's a theory about what's happening, though it doesn't explain everything you've reported (it suggests the 7000x12 array should have worked):
I think the problem here is that the first part of your code crops the input so its height is a multiple of 100. However your window size is 200 and these numbers should have been the same (for 0 overlap). This is because you calculate the start points of your windows with 1:step:(size(A,1)-overlap)
= 1:200:size(A,1)
which will produce a final window which does not fit within A
unless size(A,1)
is a multiple of 200.
Considering the array sizes you've tried:
Upvotes: 1
Reputation: 7175
Since your overlap for the windows is zero, the code below should work for any input array A
. Change the COLUMN_INDEX
and WINDOW_SIZE
variables to your desired column and window size.
The idea is to reshape the desired column to be WINDOW_SIZE
x N
. Then simply call the std function which operates on the columns of this reshaped array.
COLUMN_INDEX = 4;
WINDOW_SIZE = 200;
windowCount = floor( size( A, 1 ) / WINDOW_SIZE );
windowedColumn = reshape( A(1:WINDOW_SIZE*windowCount,COLUMN_INDEX), WINDOW_SIZE, windowCount );
stdVals = std( windowedColumn );
Upvotes: 0