How to compute Permutation with repetition, (One Value From Each Vector Only)?
5 views (last 30 days)
Show older comments
I’m trying to create a permutation with Field_A, Field_B, Field_C, taking a value from each field once a time to create a vector (Answer) with 3 elements . Field_A = [50 60 70 80] Field_B = [ 10 30 90 100] Field_C = [100 500 700 1000] Only one value should be selected from each field to create all possible answers (Vector of three elements). The answers should be 3 values only such as below Answer1 = [Field A = 50 Field B =10 Field C =100] Answer2 = [Field A =70 Field B=90 Field C =100] The order can be changed as long each field is chosen Answer1 = [Field B = 10 Field A=50 Field C =100] However, I do not want two answers from the same field to be chosen as [Field A = 50 Field A=60 Field B= 10].
0 Comments
Accepted Answer
Jos (10584)
on 22 Feb 2018
A = [50 60 70 80] ;
B = [10 30 90 100] ;
C = [100 500 700 1000] ;
% get all combinations by taking elements from A, B and C
[aa,bb,cc] = ndgrid(A,B,C) ;
R = [aa(:) bb(:) cc(:)] ;
% but now you also want to permute the three columns, that is switch A, B and C
c = perms(1:3)
R2 = arrayfun(@(k) R(:,c(k,:)), 1:size(c,1),'un',0) ;
R2 = cat(1,R2{:})
0 Comments
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!