MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   生成名称向量MATLAB (https://www.labfans.com/bbs/showthread.php?t=26540)

poster 2019-12-14 20:13

生成名称向量MATLAB
 
我将如何生成像

x1,x2,x3,x4,...,xn 问题是串联','和'x'字符

n=100 A = (1:n);

[B]回答:[/B]

要生成字符串'x1,x2'等,您可以使用[URL="http://www.mathworks.com/help/techdoc/ref/repmat.html"]REPMAT[/URL]为[URL="http://www.mathworks.com/help/techdoc/ref/sprintf.html"]SPRINTF[/URL]创建掩码, [URL="http://www.mathworks.com/help/techdoc/ref/sprintf.html"]如下[/URL] [URL="http://www.mathworks.com/help/techdoc/ref/repmat.html"]所示[/URL] :

n = 5; mask = repmat('x%i,',1,n); out = sprintf(mask,1:n); out = out(1:end-1) out = x1,x2,x3,x4,x5 请注意,如果您实际上要创建包含字符串'x1','x2'等的向量,则可以使用[URL="http://www.mathworks.com/help/techdoc/ref/arrayfun.html"]ARRAYFUN[/URL]生成单元格数组:

out = arrayfun(@(x)sprintf('x%i',x),1:n,'uniformOutput',false) out = 'x1' 'x2' 'x3' 'x4' 'x5'

[url=https://stackoverflow.com/questions/5240780]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 12:46

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.