[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Naming variables using existing variables....
From: |
Andy Buckle |
Subject: |
Re: Naming variables using existing variables.... |
Date: |
Mon, 28 Feb 2011 16:30:22 +0000 |
On Mon, Feb 28, 2011 at 3:51 PM, Christoph Ellenberger
<address@hidden> wrote:
> Hi,
>
> I know it is not the most efficient code ... but something like
> for i=1:n
> eval(cstrcat("A_",num2str(i)),"= rand("num2str(i)","num2str(i)");");
> end
> would probably work.
> Greetings
> Christoph
eval is best avoided, if possible. How about storing your matrices in a struct?
m-file:
---------------------------
n=100;
tic
A=struct();
for i=1:n
A=setfield(A,sprintf("A_%i",i),rand(i,i));
end
toc % Elapsed time is 0.078125 seconds.
tic
for i=1:n
eval( cstrcat("A_",num2str(i),"= rand(",num2str(i),",",num2str(i),");" ));
end
toc % Elapsed time is 1.09375 seconds.
---------------------------
After running
---------------------------
>A_3
A_3 =
0.46502 0.75950 0.26443
0.57791 0.26524 0.34618
0.20305 0.47766 0.48955
>A.A_3
ans =
0.206875 0.515479 0.431760
0.859863 0.358284 0.788745
0.094583 0.944578 0.079561
--
/* andy buckle */
Re: Solved: Naming variables using existing variables...., bleagos, 2011/02/28