フィルターのクリア

Forming matrix with string

1 回表示 (過去 30 日間)
Sang Heon Lee
Sang Heon Lee 2017 年 9 月 23 日
回答済み: Walter Roberson 2017 年 9 月 24 日

How can I make function that forms a matrix with string? If string is 'idk' and number is 3 the output should be sth like this [idk,idk,idk;idk,idk,idk;idk,idk,idk], matrix of 'idk' with 3 rows and colums.

回答 (2 件)

Image Analyst
Image Analyst 2017 年 9 月 24 日
編集済み: Image Analyst 2017 年 9 月 24 日
% Make the 9 strings into a cell array:
ca = {'idk','idk','idk';'idk','idk','idk';'idk','idk','idk'}
You'll see:
ca =
3×3 cell array
{'idk'} {'idk'} {'idk'}
{'idk'} {'idk'} {'idk'}
{'idk'} {'idk'} {'idk'}
If the 3 is really a variable, and you want an actual function instead of a single line of code, then you can do it like this, using repmat():
function ca = MakeCellArray(numRows)
% Make the 9 strings into a cell array:
oneCell = {'idk'}; % Make just one for a start.
% Turn it into a 3x3 array of cells
ca = repmat(oneCell, [numRows, numRows])

Walter Roberson
Walter Roberson 2017 年 9 月 24 日
function result = YourFunction
result = repmat(string('ijk'), 3, 3);
This requires R2016b or later.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by