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 Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by