Extraction a certain part out of Matrix
7 views (last 30 days)
Show older comments
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?
0 Comments
Answers (3)
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
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.
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)
0 Comments
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;
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!