Group repeated values in an array and mention the repeated value with position

4 ビュー (過去 30 日間)
ASHA PON
ASHA PON 2022 年 1 月 4 日
コメント済み: ASHA PON 2022 年 1 月 4 日
I need to group the repeated value in an array. After grouping the results should be
group = repeated value
position
Example:
a=[ 6 6 5 6 5 6 6]
Expected result:
group 1 = 5
3 5
group 2 = 6
1 2 4 6 7
Thank you in advance
  2 件のコメント
Rik
Rik 2022 年 1 月 4 日
What have you tried so far? I would try something with unique and ismember, or with histcounts.
ASHA PON
ASHA PON 2022 年 1 月 4 日
This is the code i have tried and results obtained are below
Code:
mxvalue=[6 6 5 6 5 6 6]
[out,i]=sort(maxvalue);
out1=diff(find([1,diff(out)]));
out2=[out1,numel(maxvalue)-sum(out1(:))];
out3=mat2cell(i,1,out2);
celldisp(out3);
Result:
out3{1} =
3 5
out3{2} =
1 2 4 6 7
In this result i'm able to get the positions of repeated value, but the repeated value is not displayed

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

採用された回答

KSSV
KSSV 2022 年 1 月 4 日
編集済み: KSSV 2022 年 1 月 4 日
a=[ 6 6 5 6 5 6 6] ;
[c,ia,ib] = unique(a) ;
for i = 1:length(c)
fprintf('Group %d = %d\n',i,c(i))
idx = find(ib==i)' ;
disp(idx)
end
Group 1 = 5
3 5
Group 2 = 6
1 2 4 6 7
  1 件のコメント
ASHA PON
ASHA PON 2022 年 1 月 4 日
Thank you for the support. This is what exactly i needed. The code works fine. Thank you once again

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by