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.
  1 件のコメント
Volkan Yangin
Volkan Yangin 2017 年 5 月 9 日
Thank you Jan Simon.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by