查看单个帖子
旧 2019-12-14, 20:13   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 生成名称向量MATLAB

我将如何生成像

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

n=100 A = (1:n);

回答:

要生成字符串'x1,x2'等,您可以使用REPMATSPRINTF创建掩码, 如下 所示

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'等的向量,则可以使用ARRAYFUN生成单元格数组:

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

更多&回答...
poster 当前离线   回复时引用此帖