Translated by

이 페이지의 내용은 영어에서 자동 번역되었습니다. 자동 번역 기능을 해제하려면 "<a class="turn_off_mt" href="#" onclick="window._kiq.push(['set', { 'event': 'Turn off MT' }]);">여기</a>"를 클릭하십시오.

Compare matrix element without loops

Asked by shdotcom shdotcom

shdotcom shdotcom (view profile)

on 4 May 2019
Latest activity Commented on by dpb

dpb (view profile)

on 4 May 2019
Accepted Answer by dpb

dpb (view profile)

Hi,
Is there any way to get same result without using loops?
G = [5 8; 8 5; 3 9; 7 3; 1 4; 5 10; 6 7; 4 10; 4 7; 1 6];
n = 2;
nG = size(G,1);
for ii=1:nG
zz =1;
isDom = [];
for kk=1:n
for jj=1:nG
if ii ~= jj
isDom(zz) = G(ii,kk) < G(jj,kk);
zz = zz +1;
end
end
end
R(ii) = sum(isDom==1);
end

on 4 May 2019
Edited by dpb

dpb (view profile)

on 4 May 2019

Not w/o zero loops, think not, but can reduce to one...
idx=1:nG; % working index array for element logical lookup/exclusion
R=zeros(nG,1); % preallocate
for i=1:nG
isDom=(G(i,:)-G(idx~=i,:));
R(i)=sum(isDom(:)<0);
end
You can eliminate the intermediate isDom temporary if desired..."exercise for the student" :)

shdotcom shdotcom

on 4 May 2019
Thank you
dpb

dpb (view profile)

on 4 May 2019
You can also, of course, remove the explicit loop via arrayfun, but the loop is still there and resulting code is somewhat obfuscated and may well be slower, besides...