필터 지우기
필터 지우기

selectively replace elements in vector

조회 수: 1 (최근 30일)
Srinivas
Srinivas 2013년 1월 23일
I need to replace some of the values in a vector example
a = [ 2 2 2 2 3 3 3 3 7 7 4 4 4 4 4 7 7 7 7 7 7 1 1 1 ]
I need to replace only the 7's occurring between 3 and 4 , and put 3 or 4 there
thanks in advance.
  댓글 수: 4
Walter Roberson
Walter Roberson 2013년 1월 23일
So if you have one or more 7's that is immediately preceded with a 3 and immediately followed by a 4, then the 7's are all to be changed to 4's ?
Srinivas
Srinivas 2013년 1월 23일
only the 7's that are preceded by 3 and followed by 4 are to changed to 7 , other 7's to be left as it is .
Thanks

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

채택된 답변

Daniel Shub
Daniel Shub 2013년 1월 23일
There are too many edge cases to give you a complete answer (and I don't want to do your work for you). Walter has tried to get at some of the edge cases, but your answer does not clarify the problem.
Given
a = [ 2 2 2 2 3 3 3 3 7 7 4 4 4 4 4 7 7 7 7 7 7 1 1 1 ]
you can find all the indices which are some value x with
find(a == x);
You probably need to to find all the indices that are 3, 4, and 7, and then look and see if there are any 7's between the 3's and 4's that meet your rules. You can create a set of nested loops that check each 3, 4, and 7 index. In the end you should create an array that has the indices of the 7's you want to replace
y = [9, 10];
You can then replace them with
a(y) = 4;
There are probably faster computational ways to solving the problem than nest for loops, but get the nested loops will potentially help you see the problems.
  댓글 수: 3
Srinivas
Srinivas 2013년 1월 23일
Thanks Daniel and Walter
Daniel Shub
Daniel Shub 2013년 1월 24일
@Walter that is a nicer approach.

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

추가 답변 (0개)

카테고리

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