Can anyone check my code please?

1 Ansicht (letzte 30 Tage)
Safia
Safia am 27 Okt. 2022
Kommentiert: KALYAN ACHARJYA am 28 Okt. 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?
thanks in advance

Antworten (1)

KALYAN ACHARJYA
KALYAN ACHARJYA am 27 Okt. 2022
Bearbeitet: KALYAN ACHARJYA am 27 Okt. 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
  3 Kommentare
Safia
Safia am 27 Okt. 2022
Bearbeitet: Safia am 27 Okt. 2022
Hi!
i already do that, i don't need the index of same values in whole matrix, just the indexes of same value in the same row (A1 and A2 have same dimension) ,in order to keep or replace by"0" this element in the initial matrices A1,A2. All this based on two vectors D1,D2.
KALYAN ACHARJYA
KALYAN ACHARJYA am 28 Okt. 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by