필터 지우기
필터 지우기

addition of two matrix's with constraints

조회 수: 3 (최근 30일)
reshdev
reshdev 2014년 9월 13일
댓글: Andrei Bobrov 2014년 9월 13일
Hello,
i have following code which i want to modify ....
function new_fitness
W =[0 0 0 3;0 0 0 0;0 0 0 0; 3 0 0 0 ];
cost=[1 1 1 1;1 1 1 1;1 1 1 1;1 1 1 1];
FC= [10 10 10 10;10 10 10 10;10 10 10 10;10 10 10 10];
Fn = cost.*W+(W>0).*FC
disp(Fn);
end
my output is---
Fn =
0 0 0 13
0 0 0 0
0 0 0 0
13 0 0 0
but i want to modify it such a way that particular FC element is added only once if both W(i,j) and W(j,i)>0. then my output will be like---
Fn =
0 0 0 13
0 0 0 0
0 0 0 0
3 0 0 0
Thank You

채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 9월 13일
out = W.*cost;
[ii,jj] = find(W > 0);
ij = sortrows([ii,jj]);
jj = sub2ind(size(W),ij(1,1),ij(1,2));
out(jj) = out(jj) + FC(jj);
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2014년 9월 13일
Wt = W.';
t1 = W > 0 & Wt == 0;
t2 = W > 0 & Wt > 0;
Fn(t1) = Fn(t1) + FC(t1);
Fn(t2) = Fn(t2) + FC(t2)/2;

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by