Remove duplicates only for specific values

A = [1,2,2,3,3,4,5,5,5,6];
value_to_remove = [2,3];
I have an array A, which contains some duplciates. I want to remove part of these duplicates, i.e. remove duplcates of 2 and 3, but leave duplicates of other elements unaffected.
The desired output is:
desired_output = [1,2,3,4,5,5,5,6];
Does someone know how to do this in a simple way?

回答 (1 件)

SHIVAM KUMAR
SHIVAM KUMAR 2020 年 12 月 15 日
編集済み: SHIVAM KUMAR 2020 年 12 月 15 日

0 投票

After using that array of elements to value_to_remove .
A = [1,2,2,3,3,4,5,5,5,6];
value_to_remove = [2,3];
is_repeting=zeros(1,length(value_to_remove));
index=[];
for i=1:length(A)
for j=1:length(value_to_remove) %To get the index of elements to remove
if A(i)==value_to_remove(j)
if is_repeting(j)>0 %If it had occured once already
index=[index i]; %Save the index
else
is_repeting(j) =is_repeting(j) +1 ; %Otherwise increase the count for that term
end
end
end
end
B=[];
for i=1:length(A)
if i~=index(1:end)
B=[B A(i)];
end
end
B %Will display required contents of A if put without semicolon . The required matrix.
Here B will have the result.

3 件のコメント

SHIVAM KUMAR
SHIVAM KUMAR 2020 年 12 月 15 日
編集済み: SHIVAM KUMAR 2020 年 12 月 15 日
I hope this is simple enough. As could not think of any simpler approach.
SHIVAM KUMAR
SHIVAM KUMAR 2020 年 12 月 15 日
You can call this as a function with those two vectors as arguments too to keep it straight.
Xingwang Yong
Xingwang Yong 2020 年 12 月 16 日
I'll try it, thanks SHIVAM

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

カテゴリ

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

製品

タグ

質問済み:

2020 年 12 月 15 日

コメント済み:

2020 年 12 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by