index of an array multiple same element

How can find the index of an array element if it have element have the same value for example a=[7 8 8 2 5 6 6 2 6 8] what is the index of third 8 and 6. I used find(A==8,1, firs) this gave the first one only, and how can I know the element have more than one value.

2 件のコメント

madhan ravi
madhan ravi 2019 年 2 月 25 日
what’s your expected output?
Arkanra Kadhum
Arkanra Kadhum 2019 年 2 月 25 日
the index of 7 is 1
first 8 is 2, second 8 is 3, third 8 is 10
first 6 is 6, second 6 is 7, third 8 is 9
i

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

回答 (3 件)

Stephen23
Stephen23 2019 年 2 月 26 日

1 投票

>> a = [7,8,8,2,5,6,6,2,6,8];
>> U = unique(a) % the order is the same as C.
U =
2 5 6 7 8
>> [R,C] = find(U(:)==a);
>> C = accumarray(R,C,[],@(v){v});
>> C{:} % same order as U.
ans =
4
8
ans =
5
ans =
6
7
9
ans =
1
ans =
2
3
10
Andrei Bobrov
Andrei Bobrov 2019 年 2 月 25 日
編集済み: Andrei Bobrov 2019 年 2 月 26 日

0 投票

[a1,~,ii] = unique(a,'stable');
out = [num2cell(a1(:)),accumarray(ii,(1:numel(ii))',[],@(x){sort(x)})];

2 件のコメント

Arkanra Kadhum
Arkanra Kadhum 2019 年 2 月 25 日
Hi Andrei
I try this code
a1=7; a2=8; a3=8; a4=2; a5=5 ;a6=6; a7=6; a8=2;a9=6; a10=8;
A=[ a1, a2, a3, a5, a4, a6,a7,a8,a9,a10];
[a1,~,ii] = unique(A,'stable');
out = [num2cell,a1(:)accumarray(ii,(1:numel(ii))',[],@(x){x})];
[a2,~,ii] = unique(A,'stable');
out = [num2cell,a2(:)accumarray(ii,(1:numel(ii))',[],@(x){x})];
I gate this error
out = [num2cell,a1(:)accumarray(ii,(1:numel(ii))',[],@(x){x})];
Error: Unexpected MATLAB expression.
Andrei Bobrov
Andrei Bobrov 2019 年 2 月 26 日
I'm fixed my typo in answer:
[a1,~,ii] = unique(a,'stable');
out = [num2cell(a1(:)),accumarray(ii,(1:numel(ii))',[],@(x){sort(x)})];

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

madhan ravi
madhan ravi 2019 年 2 月 25 日
編集済み: madhan ravi 2019 年 2 月 25 日

0 投票

u=unique(a);
C=arrayfun(@(x) find(a==u(x)),1:numel(u),'un',0);
%celldisp(C)

1 件のコメント

madhan ravi
madhan ravi 2019 年 2 月 25 日
If you want the order to be preserved then use 'stable' as an option in unique.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2019 年 2 月 25 日

回答済み:

2019 年 2 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by