필터 지우기
필터 지우기

Replacing repeated elements in a matrix

조회 수: 1 (최근 30일)
Danish Nasir
Danish Nasir 2021년 9월 4일
댓글: Danish Nasir 2021년 9월 4일
Suppose i have matrix A=[ 8 8 8 8 7 7 7 4 2 17 18 18]
If a number is repeated , then I want to replace the last repeated element by a number say 5. The final matrix wil be
B= [8 8 8 5 7 7 5 4 2 17 18 5].
PLs suggest a code to generate B matrix?
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 9월 4일
Suppose that
A=[ 8 8 8 8 7 7 8 4 2 17 18 18]
then would you want [ 8 8 8 5 7 5 8 4 2 17 18 5] or would you want [ 8 8 8 8 7 5 5 4 2 17 18 18] ? In other words is it "last in each run of 2 or more" or is it "last out of the entire vector" ? Oh yes an what about [8 3 8] -- should that be left alone because there are no places with consecutive values that are the same, or should it become [8 3 5] ?
Danish Nasir
Danish Nasir 2021년 9월 4일
It is similar to the proportion question which you answered. In this case wherever a repetition ,then i have to proportionate.
Here 8 8 8 8 is a proportion for which sum be 25 . So i want last 8 to be replaced by 1 . So the digits will be 8 8 8 1. Next 7 7 7 to be converted to 7 7 5.
Now the tough part is that next three digits (4 2 17) will remain same as they are not repeating which means they are not required to be proportinate.

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

채택된 답변

Matt J
Matt J 2021년 9월 4일
편집: Matt J 2021년 9월 4일

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by