如何在 MATLAB 中找到所有排列(带重复)? [英] How to find all permutations (with repetition) in MATLAB?
本文介绍了如何在 MATLAB 中找到所有排列(带重复)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有 4 个字母并且我想将它们排列在 3 个位置(允许重复),那么我将有 43=64 个可能的排列.我如何计算和打印它们?
Suppose I have 4 letters and I want to arrange them in 3 places (repetition allowed), so I would have 43=64 possible permutations. How can I compute and print them?
推荐答案
Simplifying Amro 的回答,你可以使用这个:
Simplifying Amro's answer, you could use this:
%// Sample data
x = 'ABCD'; %// Set of possible letters
K = 3; %// Length of each permutation
%// Create all possible permutations (with repetition) of letters stored in x
C = cell(K, 1); %// Preallocate a cell array
[C{:}] = ndgrid(x); %// Create K grids of values
y = cellfun(@(x){x(:)}, C); %// Convert grids to column vectors
y = [y{:}]; %// Obtain all permutations
Matrix y
应该存储你想要的排列.
Matrix y
should store the permutations you're after.
这篇关于如何在 MATLAB 中找到所有排列(带重复)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文