필터 지우기
필터 지우기

How to find repeated values in a column vector

조회 수: 2 (최근 30일)
tiwwexx
tiwwexx 2019년 6월 12일
댓글: tiwwexx 2019년 6월 14일
Hello, I'm trying to find every place where a value (1) repeats in a vector and then take only the second time the value repeats. For example if I get the vector (0110011101) I want to get out (0010001000).

채택된 답변

Monika Phadnis
Monika Phadnis 2019년 6월 13일
Try following this approach:
Convert vector to string using sprintf. Use regexp to search for repeating 1's. Replace the 1's for 0's except second one.
Try this code:
str = sprintf('%d',vector_name);
[start_index,end_index] = regexp(str,'11[1]*');
str(start_index) = '0' %converting the first 1 of repetition to zero
idx = find(end_index == start_index+1) % we check for double 1 occurence because we don't need to replace at that location
start_index(idx) = [];
end_index(idx) = []; % remove the double 1 occurence indices
str(start_index+2:end_index) = '0'; % converting to 0's for the other patterns
Hope this helps.
  댓글 수: 1
tiwwexx
tiwwexx 2019년 6월 14일
That works perfect! Thanks for the reply.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by