Generate a N length vector from a M length list (permutation)
1 次查看(过去 30 天)
显示 更早的评论
I have want to generate a 5-element vector:
A = [a1, a2, a3, a4, a5]
such that each element (a1...a5) could take on one of the three values [1, 2, 3];
There should be 3^5 number of outputs.
It should be easy but i have been trying for the last 90 minutes and got no luck.
4 个评论
回答(2 个)
Walter Roberson
2019-9-2
[Ac{1:5}] = ndgrid(1:3);
result = cell2mat(cellfun(@(M) M(:), Ac, 'uniform', 0));
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!