How to compute Permutation with repetition, (One Value From Each Vector Only)?

4 次查看(过去 30 天)
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].

采纳的回答

Jos (10584)
Jos (10584) 2018-2-22
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 个)

类别

Help CenterFile Exchange 中查找有关 Frequently-used Algorithms 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by