How to define and assign a variable?

1 回表示 (過去 30 日間)
Partha
Partha 2015 年 4 月 2 日
コメント済み: Star Strider 2015 年 4 月 3 日
I have n no of variable starting with 'VAR' and VAR1 = [2 5 4 8 9 3 6 4 1 0 3 5], VAR2 = [4 7 4 0 9 3 3 4 1 2 7 5], VAR3 = [2 5 3 8 9 6 6 4 8 0 1 5] and so on. How can i define the variable by one command where i want to use i (i = 1,2,3,4,.....) and then assign the respective value in corresponding variable?
  1 件のコメント
Andrew Newell
Andrew Newell 2015 年 4 月 2 日
Are you assigning a vector to each variable? Do you have some algorithm for deciding what numbers are assigned to each variable?

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2015 年 4 月 2 日
If you want to put them all in a matrix (good programming practise), use a loop and eval:
VAR1 = [2 5 4 8 9 3 6 4 1 0 3 5];
VAR2 = [4 7 4 0 9 3 3 4 1 2 7 5];
VAR3 = [2 5 3 8 9 6 6 4 8 0 1 5];
NrVAR = 3;
for k1 = 1:NrVAR
VARmat(k1,:) = eval(sprintf('VAR%d',k1));
end
  2 件のコメント
Partha
Partha 2015 年 4 月 3 日
Many many thanks for the solution.
Star Strider
Star Strider 2015 年 4 月 3 日
My pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDatabase Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by