matching based on some conditions
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
madhan ravi
on 6 Jul 2020
Z = (D2 <= CD(T)) .* T % are you sure about your desired result? , or am I missing some crucial information here
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!