필터 지우기
필터 지우기

Sum of elements of a matrix such that no row or column repeats

조회 수: 1 (최근 30일)
Lovish Goyal
Lovish Goyal 2021년 5월 21일
댓글: Torsten 2021년 5월 21일
If i choose an element of a matrix than the next element used should be from different row and different column. If there is a nxn matrix then the sum of n elements of the matrix should come from the elements of different row and different column.

채택된 답변

Torsten
Torsten 2021년 5월 21일
p = randperm(n);
sum = 0.0;
for k = 1:n
sum = sum + a(k,p(k));
end
where a is the nxn matrix.
  댓글 수: 4
Lovish Goyal
Lovish Goyal 2021년 5월 21일
Is there any way that we can also reject all the main diaginal elements. like k should not be equal to p(k).
Torsten
Torsten 2021년 5월 21일
p = randperm(n) ;
while any(p==(1:n))
p = randperm(n);
end
% Rest is the same

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by