필터 지우기
필터 지우기

How to find strings of ones in a vector

조회 수: 6 (최근 30일)
Maddie
Maddie 2011년 8월 27일
I have... [3 1 1 5 7 2 1 1 1 9 10]; and I would like to find the indices for my "runs" of ones. I would like my output to be: [2 3 7 9]. (The start and end of the runs of ones).

답변 (4개)

Jan
Jan 2011년 8월 27일
Another solution:
x = [3 1 1 5 7 2 1 1 1 9 10];
isOne = [false, (x==1), false];
index = [strfind(isOne, [false, true]); ...
strfind(isOne, [true, false]) - 1];
index = reshape(index, 1, []);
This takes about the half time of Oleg's method - if x has 10'000 elements.
  댓글 수: 3
Fangjun Jiang
Fangjun Jiang 2011년 8월 28일
Jan, I found one solution without using strfind(). It's a little faster.
Andrei Bobrov
Andrei Bobrov 2011년 8월 28일
+1. Hi Jan! This is my favorite method, Matt Fig - first use for such tasks.

댓글을 달려면 로그인하십시오.


Fangjun Jiang
Fangjun Jiang 2011년 8월 28일
Can use find() directly. And it's faster than strfind().
x=[3 1 1 5 7 2 1 1 1 9 10];
a=diff([0 x 0]==1);
b=[find(a==1);find(a==-1)-1];
c=b(:)'
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2011년 8월 28일
Speed test results:
%%
x=rand(1,1000);y=x>0.5;
x(y)=1; x(~y)=0;
t1=0;t2=0;
for k=1:1000;
%Jan's method
tic;
isOne = [false, (x==1), false];
index = [strfind(isOne, [false, true]); ...
strfind(isOne, [true, false]) - 1];
index = reshape(index, 1, []);
t1=t1+toc;
%Fangjun's method
tic;
a=diff([0 x 0]==1);
b=[find(a==1);find(a==-1)-1];
c=b(:)';
t2=t2+toc;
end
fprintf('Jan''s time: %f\n',t1);
fprintf('Fangjun''s time: %f\n',t2);
Jan's time: 0.082744
Fangjun's time: 0.080910
Andrei Bobrov
Andrei Bobrov 2011년 8월 28일
+1

댓글을 달려면 로그인하십시오.


the cyclist
the cyclist 2011년 8월 27일
There are probably less pedantic ways, but I think this does what you want.
x = [3 1 1 5 7 2 1 1 1 9 10];
isOne = (x==1);
isSameAsPrev = diff([NaN x])==0;
isSameAsNext = diff([x NaN])==0;
indexToFirstOrLastOne = find(isOne & (not(isSameAsPrev) | not(isSameAsNext)))
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2011년 8월 28일
Need to improve to handle single 1 case. For example, if x=[1 2 1 1 1]; result should be [1 1 3 5]. Otherwise, if result is [1 3 5], won't be able to tell if x=[1 1 1 2 1]

댓글을 달려면 로그인하십시오.


the cyclist
the cyclist 2011년 8월 28일
Slightly more efficient version of my other answer:
isOne = (x==1);
isSameAsPrevOrNext = diff([NaN x NaN])==0;
isFirstOrLastOne = find(isOne & not(isSameAsPrevOrNext(1:end-1) & isSameAsPrevOrNext(2:end)));

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by