Creating all possible combination of a letter/string

26 ビュー (過去 30 日間)
Rahul J Hirur
Rahul J Hirur 2020 年 6 月 17 日
編集済み: Cris LaPierre 2021 年 7 月 7 日
Hello everyone,
I was wondering is there a way to create all possible combination of a word in terms of uppercase and lowercase.
Say there is char
dem_a='ABC'
I need output something like
OUTPUT= ['ABC', 'ABc', 'AbC', 'aBC', 'Abc', 'aBc', 'abC', 'abc'];
Is there anyway by which I can achieve this in matlab?

採用された回答

James Tursa
James Tursa 2020 年 6 月 17 日
編集済み: James Tursa 2020 年 6 月 17 日
One way to create a matrix of these character strings:
dem_a='ABC'; % assume starting string is all upper
n = numel(dem_a);
result = repmat(dem_a,2^n,1);
mask = dec2bin((0:(2^n-1))') == '1';
result(mask) = result(mask) + ('a'-'A');
or that last line could be
result(mask) = lower(result(mask));
  2 件のコメント
Rahul J Hirur
Rahul J Hirur 2020 年 6 月 18 日
Thank you.
One tiny thing though,
dem_a if it has lowercase letter. It will throw some garbage in.
It can be solved using upper() function
James Tursa
James Tursa 2020 年 6 月 18 日
Yes, e.g.,
result = repmat(upper(dem_a),2^n,1);

サインインしてコメントする。

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2020 年 6 月 17 日
There is no built-in function for this, but you can use the capabilitied of MATLAB to find the answer. Here's one way.
t = 'ABCabc';
% All possible permutations
p = perms(t);
% Only keep the unique combinations of the first 3 letters
p = unique(p(:,1:3),'rows')
% Only keep those permutations that have 'A' in the first column, 'B' in the 2nd and 'C' in the third.
% strcmpi is case insensitive
p = p(strcmpi(p,"ABC"),:)
  2 件のコメント
Kyle Tan
Kyle Tan 2021 年 7 月 6 日
編集済み: Kyle Tan 2021 年 7 月 6 日
How can I incorporate this formula into a find txt formula?
After finding all information, I need a command to find all possible combinations of a ten letter of strings of D and E only. EX: DDDDDDDDDE, EEEEDEDEDE, etc., in any order and any amount of Ds and Es. While I used the permutation formula, I found that there were 252 results, but only involving the combination of exactly 5 D's and 5 E's. If possible, is there a way to list all possible combinations AND Permutations?
Cris LaPierre
Cris LaPierre 2021 年 7 月 7 日
編集済み: Cris LaPierre 2021 年 7 月 7 日
Not sure of your data format, but I think I would take a slightly different approach here. I would use the pattern searching capabilities of MATLAB to identify the sequences you want.
% using cell arrays
C = {'DDDDDDDDDE','EEEEDEDEDE','EEEEDEDEDA'};
pat = characterListPattern("DE");
inds = count(C,pat,'IgnoreCase',true)==10;
C{inds}
ans = 'DDDDDDDDDE'
ans = 'EEEEDEDEDE'
% using string arrays
txt = ["DDDDDDDDDE","EEEEDEDEDE","EEEEDEDEDA"];
pat = characterListPattern("DE");
inds = count(txt,pat,'IgnoreCase',true)==10;
txt(inds)
ans = 1×2 string array
"DDDDDDDDDE" "EEEEDEDEDE"

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by