How can i manipulate matrix like this?

1 Ansicht (letzte 30 Tage)
Volkan Yangin
Volkan Yangin am 9 Mai 2017
Kommentiert: Volkan Yangin am 9 Mai 2017
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 Kommentar
Jan
Jan am 9 Mai 2017
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]?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 9 Mai 2017
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.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by