Andrea Ciufo
Andrea Ciufo

Reputation: 379

Error using sub2ind (line 52) Out of range subscript Matlab

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. enter image description here

Upvotes: 2

Views: 1590

Answers (2)

Mike Dinsdale
Mike Dinsdale

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:

  • 7000x4: 7000 is a multiple of 200 so this works
  • 29500x12: 29500 is not a multiple of 200 so this fails (the final element of 1:200:29500 is 29401, so the last window is 29401:29600)
  • 102047x12: 102047 gets truncated to 102000 which is a multiple of 200 so this works
  • 7000x12: this should work in the same way as 7000x4 - which is inconsistent with what you've seen!

Upvotes: 1

b3.
b3.

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

Related Questions