如何作为MATLAB数组多次重复一个字符?

时间:2022-10-24 23:44:33

Given a single string value in a MATLAB character array:

给定MATLAB字符数组中的单个字符串值:

['12 N']

How can I repeat this value X times in a new character array?

如何在新的字符数组中重复此值X次?

For example:

X = 5

X = 5

['12 N'; '12 N'; '12 N'; '12 N'; '12 N']

2 个解决方案

#1


10  

Use the repmat function:

使用repmat函数:

A = ['12 N'];
X = 5
Output = repmat(A, X, 1);

will result in a character array.

将导致一个字符数组。

Depending on your end usage, you may want to consider using a cell array of strings instead:

根据您的最终用法,您可能需要考虑使用字符串的单元格数组:

Output = repmat({A},X,1);

#2


3  

repmat is the obvious way to go, but just for the heck of it you could use kron:

repmat是显而易见的方法,但只是因为它可以使用kron:

A = ['12 N'];
X = 5
B = char(kron(A,ones(X,1)))

Silly, yes...

#1


10  

Use the repmat function:

使用repmat函数:

A = ['12 N'];
X = 5
Output = repmat(A, X, 1);

will result in a character array.

将导致一个字符数组。

Depending on your end usage, you may want to consider using a cell array of strings instead:

根据您的最终用法,您可能需要考虑使用字符串的单元格数组:

Output = repmat({A},X,1);

#2


3  

repmat is the obvious way to go, but just for the heck of it you could use kron:

repmat是显而易见的方法,但只是因为它可以使用kron:

A = ['12 N'];
X = 5
B = char(kron(A,ones(X,1)))

Silly, yes...