Creating an ordered list of vectors

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일

1 개 추천

Please do not do that. Please read this FAQ

댓글 수: 3

Zach
Zach 2011년 5월 31일
the problem is I need each vector to do a different task. I was trying to avoid having to creating higher ordered arrays. I'll work it out tho!
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일

2 개 추천

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일

1 개 추천

v=perms(1:4);
Now instead of having v1,v2... you have v(1),v(2)...

댓글 수: 1

Jan
Jan 2011년 5월 31일
Why PERMS? The OP looks for v1=[1,2,3,4] etc.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2011년 5월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by