变量名Matlab中的循环字符串 [英] Loop String in variable name Matlab

查看:466
本文介绍了变量名Matlab中的循环字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建变量并通过循环索引将变量命名为变量名称

I would like to create variables and name the variables through an index in a loop into the name of the variable

for k = 1 : 10
    A_{k} = rand(10,1);
end

我希望它创建变量

A_1 = [.43,.234.,....]
A_2
A_3
...

但事实并非如此.它只会创建一个变量A.

But it doesnt. It only creates an variable A.

如何将索引输入循环以创建单个变量?

How do I feed the index into the loop to create individual variables?

推荐答案

您可以使用分配,如下所示:

You can make variables dynamicly, using assignin, as follows:

for k = 1:10
    assignin('base', ['A_', num2str(k)], rand(10,1))
end

whos

  Name       Size            Bytes  Class     Attributes

  A_1       10x1                80  double              
  A_10      10x1                80  double              
  A_2       10x1                80  double              
  A_3       10x1                80  double              
  A_4       10x1                80  double              
  A_5       10x1                80  double              
  A_6       10x1                80  double              
  A_7       10x1                80  double              
  A_8       10x1                80  double              
  A_9       10x1                80  double              
  k          1x1                 8  double 

这篇关于变量名Matlab中的循环字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆