Can anyone check my code please?
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
KALYAN ACHARJYA
2022-10-27
编辑:KALYAN ACHARJYA
2022-10-27
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
A2=randi([1,10],[1,10]) % Just an Example
C_A=intersect(A1,A2)
[~,D1]=intersect(A1,C_A,'stable')
[~,D2]=intersect(A2,C_A,'stable')
3 个评论
KALYAN ACHARJYA
2022-10-28
#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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!