How can i manipulate matrix like this?

1 visualización (últimos 30 días)
Volkan Yangin
Volkan Yangin el 9 de Mayo de 2017
Comentada: Volkan Yangin el 9 de Mayo de 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 comentario
Jan
Jan el 9 de Mayo de 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]?

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 9 de Mayo de 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.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by