matching based on some conditions
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!