Get one element from each row but not the same column

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

Respuestas (1)

Stephen23
Stephen23 el 17 de Feb. de 2019
Editada: Stephen23 el 17 de Feb. de 2019
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 comentarios

Stephen23
Stephen23 el 21 de Feb. de 2019
Editada: Stephen23 el 21 de Feb. de 2019
@Hardi Mohammed: your code has nothing to do with my answer, and I have no idea how it is supposed to work. You originally wrote in your quetsion "matrix 3 *3 has 6 posibility. Could you help me how to show all possiblities without repeating", which is exactly what my answer does. If you want all possiblities at least use perms and get rid of the random numbers.
"result how can I avoid repetion if I run it for 24 times."
Simple: I would use my answer. It gives all permutations, just like you requested, without any duplicates. For a 3x3 matrix it has 6 output values, for a 4x4 matrix it has 24. Each output is a different sum of "... one element from each row but different colum", exactly as you requested.
I would NOT use random numbers like you are, or a loop. Total watse of time.
Stephen23
Stephen23 el 25 de Feb. de 2019
Hardi Mohammed's "Answer" moved here:
Thank you very much Stephen Cobeldick , your answer is really helpful.
the code you have written is matrix N by N
what if the we have three row and 5 column how can I change the code X = perms(1:N) + (N*(0:N-1)); % linear indices
Thank you
Stephen23
Stephen23 el 25 de Feb. de 2019
>> R = 3;
>> C = 5;
>> A = randi(9,R,C)
A =
1 9 8 3 8
4 7 8 9 3
9 1 9 1 5
>> P = perms(1:C);
>> P = unique(P(:,1:R),'rows');
>> X = P + (C*(0:R-1));
>> B = A.';
>> sum(B(X),2)
ans =
17
9
13
10
10
14
11
19
15
5
13
5
22
14
18
26
18
22
27
27
23
21
21
13
13
13
17
24
16
20
26
18
22
20
12
12
8
16
12
19
19
15
20
12
16
15
7
15
13
21
13
24
24
16
25
17
17
26
18
26
Thank you very much Stephen Cobeldick , it works well but it gives error when I change the column number for example if I change it to 20 it gives me error about the P = perms(1:C);
what else can be used instead of perms for large coulumn number?
the error I get:
Maximum variable size allowed by the program is
exceeded.
Error in ts (line 7)
P = perms(1:C);
I appreciate your help
Stephen23
Stephen23 el 26 de Feb. de 2019
Editada: Stephen23 el 26 de Feb. de 2019
"what else can be used instead of perms for large coulumn number?"
The code I gave you (using perms) will be effective up to about C=10. Although it is simple and uses only inbuilt functions it is very inefficient because it generates all permutations before discarding some/most of them. For larger C you will have to build up only the required permutations (i.e. rather than creating all and discarding superfluous ones). To do this you could download and try some FEX submissions, e.g.:
And use it simply like this:
P = combinator(C,R,'p');
This will work for much larger C, and you can increase the range even more by defining C to be an unsigned integer (read its help to know more). Of course eventually this will also run out of memory, at which point you will need to reconsider your algorithm.
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 el 26 de Feb. de 2019
@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 !

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 17 de Feb. de 2019

Comentada:

el 26 de Feb. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by