How to replace duplicate elements with the elements of the second array?

Hi , I have two arrays , one with duplicate elements and I want to keep the first occurance of the element and replace other repeated values with the elements of the second array. Here is the example
a=[23 34 56 78 100 10 12];
b= [2 2 2 4 6 0 0 11 11 11 6];% contains repeating elements
iwant=[2 23 34 4 6 0 56 78 11 100 10 12]

6 个评论

How does 12 fit into the output you want? Do you want to attach the remaining elements from a to appended to the modified b array?
In b, 6 comes twice and therefore replaced by 12.
b= [2 2 2 4 6 0 0 11 11 11 6]
That I understood, but it doesn't make sense. iwant has more elements than b -
b= [2 2 2 4 6 0 0 11 11 11 6]
b = 1×11
2 2 2 4 6 0 0 11 11 11 6
iwant=[2 23 34 4 6 0 56 78 11 100 10 12]
iwant = 1×12
2 23 34 4 6 0 56 78 11 100 10 12
Either iwant is written incorrectly or there's something missing
Example
Set=[0 1,2,...100]; b=[number occurs once & repeating numbers]
a=[missing numbers];
i want = [elements of b that occurs once plus repeating elements replced by missing numbers]. ofcourse iwant is larger than a or b.
Size(iwant)= Size(Set) (order of elements will be different.
Let's go through the elements -
a=[23 34 56 78 100 10 12];
b= [2 2 2 4 6 0 0 11 11 11 6];
Starting with 2nd index
index 2, b(2) appears before, replaced by a(1), [2 23 2 4 6 0 0 11 11 11 6]
index 3, b(3) appears before, replaced by a(2), [2 23 34 4 6 0 0 11 11 11 6]
index 4, b(4) doesn't appear before
index 5, b(5) doesn't appear before
index 6, b(6) doesn't appear before
index 7, b(7) appears before, replaced by a(3), [2 23 24 4 6 0 56 11 11 11 6]
index 8, b(8) doesn't appear before
index 9, b(9) appears before, replaced by a(4), [2 23 24 4 6 0 56 11 78 11 6]
index 10, b(10) appears before, replaced by a(5), [2 23 24 4 6 0 56 11 78 100 6]
index 11, b(11) appears before, replaced by a(6), [2 23 24 4 6 0 56 11 78 100 10]
Now, appending remaining elements of a (7 to end) to b, [2 23 24 4 6 0 56 11 78 100 10 12]
which is different from iwant=[2 23 34 4 6 0 56 78 11 100 10 12]
As @Voss has stated in their answer.
It seems that there's a mistake in iwant. I insist you clarify this.

请先登录,再进行评论。

 采纳的回答

Maybe something like this?
a = [23 34 56 78 100 10 12];
b = [2 2 2 4 6 0 0 11 11 11 6];% contains repeating elements
yougot = b;
a_idx = 1;
for ii = 1:numel(b)
if any(b(1:ii-1) == b(ii))
yougot(ii) = a(a_idx);
a_idx = a_idx+1;
end
end
if a_idx <= numel(a)
yougot = [yougot a(a_idx:end)];
end
disp(yougot);
2 23 34 4 6 0 56 11 78 100 10 12
iwant = [2 23 34 4 6 0 56 78 11 100 10 12];
disp(iwant);
2 23 34 4 6 0 56 78 11 100 10 12
Note that the 11 and the 78 are switched in iwant vs yougot. I think that's a mistake in iwant, according to the description of the algorithm you gave.

更多回答(1 个)

Hi,
If i get the question correctly, you want to append the remaining unused elements of a. I wrote a basic idea, maybe it can be optimized in some way!
a=[23 34 56 78 100 10 12];
b= [2 2 2 4 6 0 0 11 11 11 6];% contains repeating elements
changed = 1; % flag initialization
j = 1; % a vector index
for i=2:length(b)
if changed == 1
temp = b(i-1);
end
if b(i) == temp
b(i) = a(j);
j = j+1;
changed = 0;
else
changed = 1;
end
end
for j=j:length(a)
b = [b a(j)];
end
Hope it is helpful,

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by