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

1 次查看(过去 30 天)
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