How can i manipulate matrix like this?

조회 수: 1 (최근 30일)
Volkan Yangin
Volkan Yangin 2017년 5월 9일
댓글: Volkan Yangin 2017년 5월 9일
EDIT: I have solved the problem with using if-end loop, but if you share alternative solutions if you have, i will be very happy. Thanks.
Hi everbody,
I have a matrix like under the this text:
A=[1 1 2 2 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4 5 5 1 1 2 2 3 3 4 4 5 5 ]
To transform the some numbers, which were written with bold type, to [4 3 2]; what kind of a code can be written?
If the numbers decrease from 6 to 1 or 5 to 1, some values should be change.
Modified matrix should be:
A=[1 1 2 2 3 3 4 4 5 4 3 2 1 1 2 2 3 3 4 4 3 2 1 1 2 2 3 3 4 4 5 5 ];
after the operation.
Thanks for answers.
  댓글 수: 1
Jan
Jan 2017년 5월 9일
Please explain the details. "If the numbers decrease from 6 to 1 or 5 to 1, some values should be change" is not exactly enough to implement a specific code. What should happen for [5 6 6 6 1]? Or [5 5 5 1]? Or [3 3 6 6 1]?

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

채택된 답변

Jan
Jan 2017년 5월 9일
If there is a small number of possible repleacements only -perhaps 10-, use strrep:
toRep = [5 6 6 1];
Rep = [4 3 2 1];
data = strrep(data, toRep, Rep);
If the problem concerns many different patterns, define the pattern exactly at first. Then a general algorithm can be found.

추가 답변 (0개)

카테고리

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