I have an array in my matlab code, and what I want to do is, lets say the array is:
wf_array = [1 2 3 4 5 6 7 8 9 10 11 25 26 28 29 45 46 47 48 49 50 51 52
Well, your cumsum
will count up when there is at least one value that's not consecutive. This means, each of your streams will have a different number in x
. mode
, on the other hand, finds only the most frequent number, and the smallest one of them (so if there are several streams of same length, the first one will be taken). According to the documentation for mode
, "the mode
function is not suitable for finding peaks in distributions having multiple modes."
So there's 3 ways to tackle this problem. The first is to alter x in a way that makes all streams have the same number. The second is to replace mode
with something that can find multiple modes. The third is to take a totally different approach and split the array.
I'll take the third one because I think it's the "easiest".
w = find( diff(v)~=1 ); %// this contains all indices where the data jumps
x = diff([0 w length(v)]); %// this contains the lengths of the streams
y = mat2cell(v,1,x); %// creates a cell array with each consecutive stream
z = y( cellfun('size',y,2)>=10 ) %// picks those cells with length of at least 10
final = cell2mat(z); %// revert it back to an array
Note that this attemp is not robust and will only work on arrays of size 1xN
. If your original input array is differently shaped, you should reshape it first:
v = input_array_of_arbitrary_size(:)'; %'// reshapes into row vector columnwise
For example, if you input is
in = [1 4 7
2 5 8
3 6 9];
the result will be:
v = in(:)' %'//random comment to fix SO's syntax highlighting
v =
1 2 3 4 5 6 7 8 9
This works for arrays of any dimension and will append each dimension at the end. For details, check out linear indexing.