Remove some numbers from an array and replace them with zero?

2 ビュー (過去 30 日間)
Rikke
Rikke 2019 年 4 月 10 日
コメント済み: Rikke 2019 年 4 月 10 日
Lets say the array is like this:
A = [4 4 4 4 6 6 6 6 6 6 2 2 3 3 3 5 5 5 5 5 3 3 3];
How could I change the matrix to be like this:
B = [4 0 0 0 6 0 0 0 0 0 2 0 3 0 0 5 0 0 0 0 3 0 0];
I want to represent each new number in the array but not more than once. The numbers may occure several times as number three in this example.

採用された回答

Stephen23
Stephen23 2019 年 4 月 10 日
編集済み: Stephen23 2019 年 4 月 10 日
>> A = [4,4,4,4,6,6,6,6,6,6,2,2,3,3,3,5,5,5,5,5,3,3,3]
A =
4 4 4 4 6 6 6 6 6 6 2 2 3 3 3 5 5 5 5 5 3 3 3
>> B = A;
>> B([false,~diff(B)]) = 0
B =
4 0 0 0 6 0 0 0 0 0 2 0 3 0 0 5 0 0 0 0 3 0 0
  1 件のコメント
Rikke
Rikke 2019 年 4 月 10 日
Perfect, it worked! Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by