Get one element from each row but not the same column

2 ビュー (過去 30 日間)
Hardi Mohammed
Hardi Mohammed 2019 年 2 月 17 日
コメント済み: Stephen23 2019 年 2 月 26 日
I am trying to find the sumation of matrix zero, when I choose one element from each row but different colum,
matrix 3 *3 has 6 posibility. Could you help me how to show all possiblities without repeating.
x=zeros(3,3);
temp=0;
test=zeros(3,3);
b=sum(x,1);
for i=1:3
temp=0;
r=randperm(3);
for j=1:3
if temp~=r(j)
temp=r(j);
if sum(x(i,:))==0 && b(temp)==0
x(i,temp)=1;
end
end
end
b=sum(x,1);
end
x

回答 (1 件)

Stephen23
Stephen23 2019 年 2 月 17 日
編集済み: Stephen23 2019 年 2 月 17 日
All six permutations of sums selecting one value from each row:
>> N = 3;
>> A = randi(9,N,N)
A =
8 4 8
9 6 7
8 1 5
>> X = perms(1:N) + (N*(0:N-1)); % linear indices
>> B = A.';
>> sum(B(X),2)
ans =
22
18
19
18
16
19
  7 件のコメント
Hardi Mohammed
Hardi Mohammed 2019 年 2 月 26 日
Thank you very much,
but I used this line
P = combinator(C,R,'p');
Mtlab does not know the combinator
so what is it? How can I use it?
Stephen23
Stephen23 2019 年 2 月 26 日
@Hardi Mohammed: you have to download it. It is very simple:
  1. click on the link in my last comment.
  2. click on the big blue button "Download" (top right of the page).
  3. save the zip file anywhere.
  4. unzip the contents onto your MATLAB path (e.g. into the current directory).
  5. you are now ready to use combinator !

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by