Extraction a certain part out of Matrix

7 views (last 30 days)
Stefan
Stefan on 12 Jul 2011
Hi,
I have a matrix that contains valid and invalid data. The invalid data is already marked by a vector that has either a 1 (if it's a valid data point) or a 0 (if it's invalid):
Col1 Col2 ... Col10 ... 1 ... 1 ... 0 ... 1 ... 1 ... 1 ... 0 ... ...
The ones and zeros in Col10 are distributed irregular and I would like to extract the longest continuous row of ones in Col10.
How do I do that? Any ideas?

Answers (3)

Oleg Komarov
Oleg Komarov on 12 Jul 2011
out = findseq(X,2);
[m,idx] = max(out(:,end));
[r,c] = ind2sub(size(X),out(idx,2))
r and c are the row and column of the first one of the longest contiguos row of ones.
  2 Comments
Stefan
Stefan on 13 Jul 2011
findseq - What a handy little function!
Works great.
Is there any way to tell the function what it should look for? In my case it could be that there is more invalid date then valid data. So it might find a longer series of zeros then ones. I could just work with the output of findseq and throw away all zeros but maybe there is another clever way to tell findseq right away what to look for.
Thank you so much already!
Oleg Komarov
Oleg Komarov on 13 Jul 2011
out in the example in the first column contains the value that is repeated and the last value the number of repetitions (if more than 1). You can sortrows out by the 4th column and see which value comes first, a 0 or a 1.

Sign in to comment.


Fangjun Jiang
Fangjun Jiang on 12 Jul 2011
The code below will find the first longest continuous row of ones.
Data=rand(100,1);
index=Data>0.5;
Data(index)=1;
Data(~index)=0;
DataStr=sprintf('%d',Data)
FoundStr=textscan(DataStr,'%s','delimiter','0','MultipleDelimsAsOne',true);
d=cellfun('length',FoundStr{1});
[dMax,IndMax]=max(d);
Longst=FoundStr{1}{IndMax};
StartIndex=strfind(DataStr,Longst);
DataIndex=StartIndex(1):(StartIndex(1)+dMax-1)
SelectedData=Data(DataIndex)

Andrei Bobrov
Andrei Bobrov on 13 Jul 2011
My favorite version :) all is tired of me
X = rand(20,1)>.35
In = X(:)';
idx = [strfind([0 In],[0 1]);strfind([In 0],[1 0])];
[l,io] = max(diff(idx));
idxout = io;
lgr = l + 1;

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!