How to create a matrix of numbers from workspace variables?

조회 수: 3 (최근 30일)
Aniket Vagha
Aniket Vagha 2016년 2월 24일
답변: Stephen23 2016년 2월 24일
I have 70 double variables of size 1500x1(let's call them a1,a2....a70) loaded in workspace. I would like to create a matrix M which contains these variables as columns, so that the matrix size will be 1500x70. Is there an efficient way to do this?
  댓글 수: 1
Aniket Vagha
Aniket Vagha 2016년 2월 24일
편집: Aniket Vagha 2016년 2월 24일
I used this:
params = who('a*'); % Vars I need start with a.
Matrix = zeros(1500, 70); % 1500 and 70 obtained from array sizes.
for ii = 1:length(Params)
Matrix(:,ii) = eval(Params{ii})
end
Is there a more direct way by-passing eval?

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

답변 (1개)

Stephen23
Stephen23 2016년 2월 24일
Yes there is an efficient way to do this: don't create 70 numbered variables in your workspace.
How did you "create" them?
If it was by load, then simply call load with an output argument:
S = load(...)
If you created your 70 variables some other way then let us know and we can show you a much better way of programming.
Read my answer to know why accessing 70 variables in a loop is slow, buggy, and obfuscated way to program:

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by