Create a cell array

1 回表示 (過去 30 日間)
Cristian
Cristian 2014 年 5 月 10 日
回答済み: Image Analyst 2014 年 5 月 10 日
How to create a cell arrays consisting of various combinations of letters ABCDE? Letters can be repeated.
  1 件のコメント
Image Analyst
Image Analyst 2014 年 5 月 10 日
What about the lengths of the strings? Are 'ABBD' and 'EEECBAABBEEEDDD' both allowed (in different cells of course)? Or so they all have to be 5 characters long, like 'AAAAA' and 'AECCB'?

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

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2014 年 5 月 10 日
perms('A':'E')

Image Analyst
Image Analyst 2014 年 5 月 10 日
Try this:
clc;
numberOfCells = 10; % Whatever you want
letters = cell(numberOfCells, 1); % Initialize.
for k = 1 : numberOfCells
% Get random length between 1 and 9
thisLength = randi(9);
% Get all letters from A up to the max allowed.
% Numbers are randomly chosen so that repeats may occur.
m = 64+randi(thisLength, [1, thisLength]);
letters{k} = sprintf('%c', m);
end
% Show the cell array in the command window.
celldisp(letters);
In the command window you'll see:
letters{1} =
DBBAGED
letters{2} =
ABC
letters{3} =
AA
letters{4} =
A
letters{5} =
DBCA
letters{6} =
ADDB
letters{7} =
CDFECCD
letters{8} =
BCCB
letters{9} =
AABB
letters{10} =
FFCBEE

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by