필터 지우기
필터 지우기

how to obtain the id of change in vector

조회 수: 2 (최근 30일)
Niraj
Niraj 2012년 11월 27일
i have a vector say
v =[ 0 0 0 0 28 34 0 56 67 0 0 0 69 71 75 89 90 0]
I need to find what are the ids where there is change in value from 0 to some number or from some number to 0. So in this case the ids should be like 5 6 8 9 13 17
Is there a smart way to do it?

답변 (2개)

Walter Roberson
Walter Roberson 2012년 11월 27일
Lv = (v ~= 0);
find( Lv(1:end-1) ~= Lv(2:end) )
  댓글 수: 3
Jan
Jan 2012년 11월 27일
I assume "id" means "index".
Niraj
Niraj 2012년 11월 27일
yes,"id" means "index".

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


Jan
Jan 2012년 11월 27일
편집: Jan 2012년 11월 27일
Lv = (v ~= 0);
Index = find(diff(Lv)) + 1;
[EDITED]
Lv = [false, (v ~= 0), false];
ini = strfind(Lv, [false, true]) + 1;
fin = strfind(Lv, [true, false]);
Index = reshape([ini(:), fin(:)], 1, []);
  댓글 수: 2
Niraj
Niraj 2012년 11월 27일
편집: Niraj 2012년 11월 27일
Thanks for the answer, but it gives the output as
5 7 8 10 13 18
but i want
5 6 8 9 13 17
Jan
Jan 2012년 11월 27일
See [EDITED]

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by