# Can anyone check my code please?

1 view (last 30 days)
Safia on 27 Oct 2022
Commented: KALYAN ACHARJYA on 28 Oct 2022
Hello everyone!
at first step i have two matrices A1,A2. There are some same value existed in both A1,A2. So i extracted their indexes in two vectors.
at second step : i have two vectors D1, D2 containing values relative to these extracted values from A1,A2.
i compared values of D1 and D2 in order to change position of extracted elements as following :
if D1(i)>D2(j), i will keep value in A2 and remove it from A1.
if D1(i)=<D2(j), i will keep value in A1 and remove it from A2.
based on indexes i used this code :
for i1=1:size(I11)
for i2=1:size(I22)
for j1=1:size(I111)
for j2=1:size(I222)
if D1(I11(i1))> D2(I22(i2))
A1(I111(j1)) =0;
A2(I222(j2))=A2(I222(j2));
elseif D1(I11(i1)) =< D2(I22(i2))
A1(I111(j1))= A1(I111(j1));
A2(I222(j2))=0 ;
end
end
I111 : index of extracted value from A1.
I222 : index of extracted value from A2 .
I11 : index of relative value extracted from A1 in D1
I22 : index of relative value extracted from A2 in D2
I don't know what is wrong with my code, it doesn't work as i want.
could you please check with me?

KALYAN ACHARJYA on 27 Oct 2022
Edited: KALYAN ACHARJYA on 27 Oct 2022
Sufficints Hint: You can also do this in efficient way, I have shown work till D1 and D2, try the rest part yourself
A1=randi([1,10],[1,10]) % Just an Example
A1 = 1×10
2 2 1 7 3 2 7 5 3 2
A2=randi([1,10],[1,10]) % Just an Example
A2 = 1×10
6 9 3 6 10 3 9 9 3 8
C_A=intersect(A1,A2)
C_A = 3
[~,D1]=intersect(A1,C_A,'stable')
D1 = 5
[~,D2]=intersect(A2,C_A,'stable')
D2 = 3
KALYAN ACHARJYA on 28 Oct 2022
#Check A1 and A2, get the indices of respective elements, replace all those elements by 0
A1=randi([1,10],[1,10])
A2=randi([1,10],[1,10])
idx=A1==A2
A1(idx)=0
A2(idx)=0
Or #Replace all D1 and D2 indices element with 0
A1(D1)=0
A2(D2)=0

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by