could anyone help me how to display all the numbers inaddition to the numbers present.

I a having a system with maximum number of users to be 6.
A =[1 2;
1 3;
2 5;
2 4;
3 5;
4 6
4 5]
In each row i can get only two numbers out of 6.
But i want to display all the numbers in each row of the matrix in the following manner
[1 2 3 4 5 6;
1 3 2 4 5 6;
2 5 1 3 4 6;
2 4 1 3 5 6;
3 5 1 2 4 6;
4 6 1 2 3 5;
4 5 1 2 3 4]
So I want to reshape the matrix A inorder to get the above matrix
Could anyone please help me on this.

 Accepted Answer

>> A = [1,2;1,3;2,5;2,4;3,5;4,6;4,5]
A =
1 2
1 3
2 5
2 4
3 5
4 6
4 5
>> F = @(v)[v,setdiff(1:6,v)];
>> M = cell2mat(cellfun(F,num2cell(A,2),'uni',0))
M =
1 2 3 4 5 6
1 3 2 4 5 6
2 5 1 3 4 6
2 4 1 3 5 6
3 5 1 2 4 6
4 6 1 2 3 5
4 5 1 2 3 6

More Answers (1)

I'd divide you task in several steps:
  1. Use for loop to consider each row separately
  2. Use ismember() to identificate which numbers (1:6) are members of a current row
  3. Add to the current row number that are not members

5 Comments

i tried with the following code:
for i=1:size(A,1)
indices=find(A,i)
ismember(A,i)
end
Could you please help me on this.
I'd probably use
doc setdiff
rather than ismember. This will give you the missing 4 numbers for each row.
i also tried with the following code but unable to get the result.
A= [1 3;
2 3;
2 4;
1 5;
2 5;
1 6;
3 6;
4 6;
5 6];
b=size(A,1);
B = A;
c=A(end,:);
for i = 1:b
B(i,c(randperm(2))) = setdiff(1:b,B(i,1:2))
end
could you please help me on this.
Why are you using randperm? setdiff should return 4 values for each row, which should be assigned (in the order they are returned, so far as I can see from what you want) as columns 3 to 6 or that row of the result matrix.
could you please help me to get the following result
[1 2 3 4 5 6;
1 3 2 4 5 6;
2 5 1 3 4 6;
2 4 1 3 5 6;
3 5 1 2 4 6;
4 6 1 2 3 5;
4 5 1 2 3 4]

Sign in to comment.

Asked:

on 19 Sep 2019

Answered:

on 19 Sep 2019

Community Treasure Hunt

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

Start Hunting!