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!