matching based on some conditions

조회 수: 1 (최근 30일)
GMDI
GMDI 2020년 7월 6일
댓글: GMDI 2020년 7월 6일
Hi,
I have following D2, CD, T matrix from previous part of the code:
D2 = [1400 2200 1500];
CD = [4000 1200 1500];
T=[ 3 2 1
1 2 3
2 3 1];
Now I want a Z matrix like this:
Z=[ 2 0 1
3 0 0
0 0 0];
Logic is – D2(1,1) will be assigned to CD(1, T(1,1)) only if D2(1,1) <= CD(1,T(1,1)) and then we don’t need to check CD(1,T(1,2)) or CD(1,T(1,3))
If D2(1,1) > CD(1,T(1,1)), then D2(1,1) will be assigned to next CD(1, T(1,j)) for all j = 2 to size(T,2) ; whichever j satisfied this condition:: D2(1,1) <= CD(1,T(1,j))
Same thing for D2(1,2). It will be assigned to CD(1,T(2,1)) only if D2(1,2) <= CD(1, T(2,1))
If D2(1,2) > CD(1, T(2,1)) then D2(1,2) will be assigned to next CD(1, T(2,j)) for all j = 2 to size(T,2) ; whichever j satisfied this condition:: D2(1,2) <= CD(1,T(2,j))
Same thing for D2(1,3) .......
I also need to make sure that capacity of CD doesn’t exceed.
I currently wrote following:
Z=zeros (size(D2,2), size(CD,2));
for i=1:size(D2,2)
for j= 1:size(CD,2)
if D2(1,i) <= CD(1,T(i,j))
Z(i,j)=T(i,j);
else
Z(i,j)=0;
end
end
end
and I get
Z=[ 3 0 0
1 0 0
0 3 0];
WHICH IS DEFINITELY WRONG !!!
Can anyone give me any idea? Thanks in advance.

답변 (1개)

madhan ravi
madhan ravi 2020년 7월 6일
Z = (D2 <= CD(T)) .* T % are you sure about your desired result? , or am I missing some crucial information here
  댓글 수: 1
GMDI
GMDI 2020년 7월 6일
Thanks for your response. Yes, I am sure about desired result.
in Z : size(Z,2) will be the size(CD,2). and size(Z,1) will be the size of size(D2,2)
T(1,:)=[ 3 2 1] sequence is for D2(1), which means D2(1) will first assign to CD(3) if D2(1) <= CD(3) , if D2(1) > CD(3) then D2(1) will be assigned to either CD(2) or CD(1) whichever satisfies D2 <= CD condition...
T(2,:)= [1 2 3] sequence is for D2(2), which means D2(2) will first assigned to CD(1) if D2(2) <= CD(2), if D2(2) > CD(1) then D2(2) will be assigned to either CD(2) or CD(3) whichever satisfies D2 <= CD
same for T(3,:) =[2 3 1] sequence .....
D2 = [1400 2200 1500];
CD = [4000 1200 1500];
T=[ 3 2 1
1 2 3
2 3 1];
now from my test data: D2(1) [=1400] <= CD(3) [=1500], so Z(1,3)= 1 ; [remaining balance of CD(3)=100]
D2(2) [= 2200] <= CD(1) [=4000], so Z(1,1) = 2 ; [here the reamining 1800 of CD(1) can be used in other assignments if needed and D2 <= CD condition satisfied]
D2(3) [=1500] > CD(2) [=1200], so we cannot assign D2(3) to CD(2). Next we need to check if D2(3) <= CD(3) [=1500] , which is true. but we cannot assign D2(3) to CD(3) because we already used 1400 of CD(3) for D2(1). Next we need to check if D2(3) <= CD(1) [=4000], so Z(2,1) = 3
Final asnwer is CD(1) was assigned to D2(2) and D2(3), CD(2) was not assigned to any D2; aand CD(3) was assigned to D2(1). all these assignments are based on the sequence of each row of T; and final Z would be -
Z=[2 0 1
3 0 0
0 0 0];
If I use your line then I get :
Z=[3 0 1
1 0 3
0 0 1];
we don't have any 2 here and 3 and 1 are repeated. It (1,2,3) cannot repeated in Z matrix.
Soory if I couldn't make my first question clear. Thanks again for your answer.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by