如何在MATLAB中找到所有排列(重复)? [英] How to find all permutations (with repetition) in MATLAB?

查看:501
本文介绍了如何在MATLAB中找到所有排列(重复)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有4个字母,并且想将它们排列在3个位置(允许重复),那么我将有4 3 = 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?

推荐答案

简化 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

矩阵y应该存储您想要的排列.

Matrix y should store the permutations you're after.

这篇关于如何在MATLAB中找到所有排列(重复)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆