Creating an ordered list of vectors

36 次查看(过去 30 天)
I'm looking to create a list of vectors arbitrary in length. I was looking for something along the lines of: iterations=___ % some arbitrary number
for i=1:iterations vi=[1 2 3 4]; end
Hoping this would create the vectors v1, v2, v3, v4 ... which are all the same vector [1 2 3 4].

采纳的回答

Walter Roberson
Walter Roberson 2011-5-31
Please do not do that. Please read this FAQ
  3 个评论
Walter Roberson
Walter Roberson 2011-5-31
Generating full variable names is nearly always trouble. Use cell arrays or use a structure with dynamic field names.
Walter Roberson
Walter Roberson 2011-5-31
T1 = cellstr(num2str((1:iterations).','v%d')).';
T2 = repmat({[1 2 3 4]},1,iterations);
T = [T1;T2];
VVars = struct(T{:});
Then use VVars.v1, VVars.v2, VVars.(sprintf('v%d',192)) and so on.

请先登录,再进行评论。

更多回答(2 个)

Jan
Jan 2011-5-31
Use a CELL instead:
v = cell(1, iterations);
v(:) = {1:4}
Now you can use v{1} instead of v1. It is always better (nicer, safer, faster) to use an index as index, instead of hiding the index in the name of the variable.

Paulo Silva
Paulo Silva 2011-5-31
v=perms(1:4);
Now instead of having v1,v2... you have v(1),v(2)...

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by