Matlab: variable index size in loop

115 views Asked by At

I've been trying to figure this out myself and also searched here. The fact that I am not quite sure what the problem is isn't helping with this. I have previously found good help in answers to other people here, so I hope that you can help me.

I have a dataset (2000x1100) that consists of 1's and 0's, where each row represents one datapoint over time (columns). I am trying to find sequences of 0's for each row and exclude those that are below a specific length. I have already found helpful answers here that allowed me to do this (thanks!).

'test' is a reduced test dataset that I am using to test the code.

for j = 1:size(test,1)

testThresh(j,:) = diff([1 test(j,:)]);
startIndex = find(testThresh(j,:) < 0);
if isempty(startIndex)
   continue
   j = j+1;
end
endIndex = find(testThresh(j,:) > 0)-1; 

if numel(endIndex) == 1

    for m = 1:size(startIndex,2)

        duration = (endIndex(m)-startIndex(m))+1;  
        threshold = (duration < 10);
        startIndexC = startIndex(threshold);
        endIndexC = endIndex(threshold);
    end
else    
    duration = (endIndex(j,:)-startIndex(j,:))+1;  
    threshold = (duration < 10);
    startIndexC = startIndex(threshold);
    endIndexC = endIndex(threshold);
end

..

I would later replace the below-threshold sequences of 0's with 1's.

The code works fine until I happen to have a row that doesn't contain any 0's. It would skip to the next row as supposed to, but there it will result in an error:

 Attempted to access endIndex(2,:); index out of bounds because size(endIndex)=[1,4].

I don't really understand why this causes an error. There isn't any problem with this specific row if it isn't preceded by a no-zero-containing row. I assume that encountering a row w/o 0 does something to the allowed size of the array. I don't really know how to get around it. I can't pre-allocate because I don't know the size the variable will reach in each iteration (I tried anyway and it didn't help). I also tried to say that numel(endIndex) >= 1 otherwise skip to next step. That also didn't work. I probably made a really silly mistake, but I am still quite new to Matlab and just can't figure it out.

I hope anyone can help with this!

Any help is greatly appreciated!

Edit:

 duration = (endIndex(1,:)-startIndex(1,:))+1;

This is very stupid. I am ashamed. Thanks for pointing this out.

Just for reasons of completion since I wasn't specific enough of what I want to accomplish (sorry about that): I want to determine the start, end and length of stretches of 0's within a lot of 1's. If the length of any of these stretches is below the threshold of 10 (i.e. too short for my purposes to be considered) I want to disregard it.

0

There are 0 answers