Indexing how many times is an element repeated in a matrix in another array

1 回表示 (過去 30 日間)
Ningct12
Ningct12 2019 年 11 月 19 日
コメント済み: Ningct12 2019 年 11 月 20 日
Sorry if the wording in the title doesn't make sense. I'm still a noob to MATLAB.
Let's say I have an experiment order of A = [1 2 1 2 3 2 3 3 1].
I would like to make another array counting how many times each task is repeated in A.
The expected outcome would be B = [1 1 2 2 1 3 2 3 3].
Particularly, when 1 appeared the 2nd time in A(3), B(3) = 2. When 1 appeared the 3nd time in A(9), B(9)= 3. Same applies for the other numbers.
How can I achieve this? Any help is much appreciated. Thank you!

採用された回答

Erivelton Gualter
Erivelton Gualter 2019 年 11 月 19 日
The following piece of code solve your problem:
arr = unique(A);
for i=1:length(arr)
n = arr(i);
B(A==n) = 1:length(find(A==n));
end
B
Next time, try to show some attempt code ;)
  1 件のコメント
Ningct12
Ningct12 2019 年 11 月 20 日
I was orginally planning to count them in a for loop using counter+1, but your method is much better! Thank you for your help! :)

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

その他の回答 (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