replace the unique value of a vector

Hello,
Please help me with the following.
Consider a vector a=[2;3;3;5;5;5];
How can we replace the unique value "2" with the most populated one, i.e. "5".
The unique value can be found anywhere in the vector, in the above example it is shown as the first element.
The new vector should look like this:
b=[5;3;3;5;5;5];
Thank you very much.
Best,
Pavlos

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 9 日
編集済み: Azzi Abdelmalek 2014 年 2 月 9 日

0 投票

a=[2;3;3;5;5;5];
c=unique(a);
ii=histc(a,c)
[idx1,idx1]=min(ii)
[idx2,idx2]=max(ii)
minv=c(idx1)
maxv=c(idx2)
b=a
b(a==minv)=maxv
%or
a=[2;3;3;5;5;5];
c=unique(a);
ii=histc(a,c)
[~,jj]=sort(ii)
a(a==c(jj(1)))=c(jj(end))

その他の回答 (2 件)

Jan
Jan 2014 年 2 月 9 日
編集済み: Jan 2014 年 2 月 9 日

0 投票

a = [2;3;3;5;5;5];
[B, N, Ind] = RunLength(a);
[maxNum, maxInd] = max(N);
mostValue = B(maxInd);
uniqueInd = Ind(N == 1);
a(uniqueInd) = mostValue;
pavlos
pavlos 2014 年 2 月 9 日

0 投票

Both methods work excellent.
Thank you very much.

カテゴリ

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

質問済み:

2014 年 2 月 9 日

編集済み:

Jan
2014 年 2 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by