[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:43:54 +0000 |
On Mon, Feb 28, 2011 at 4:29 PM, Przemek Klosowski
<address@hidden> wrote:
> On 02/28/2011 10:41 AM, bleagos wrote:
>
>>> Thanks for replying so quick. The only problem with this is that I
>>> want my input to be matrices of different size depending on the
>>> local variable. So something like this:
>>>
>>> for i=1:n A_i = rand(i,i); end
>
> I think such 'synthetic' variable names almost always are a mistake.
> Octave has a perfectly good datatype for this kind of work, the cell array:
>
> for i=1:4
> A{i}=rand(i);
> end
>
> results in A =
>
> {
> [1,1] = 0.61921
>
> [1,2] =
>
> 0.13984 0.64332
> 0.55874 0.79659
>
> [1,3] =
>
> 0.85950 0.75172 0.23826
> 0.87920 0.39471 0.83086
> 0.74039 0.41796 0.25512
>
> [1,4] =
>
> 0.091380 0.514551 0.901517 0.451026
> 0.346724 0.840009 0.057729 0.225127
> 0.625270 0.867836 0.830498 0.292810
> 0.923174 0.948555 0.781887 0.082019
>
> }
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave
>
Do what Przemek said. It is even faster.
--------------------
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.
tic
for i=1:n
B{i}=rand(i);
end
toc % Elapsed time is 0.03125 seconds.
--------------------
>B{3}
ans =
0.643261 0.413923 0.220478
0.047525 0.664410 0.848572
0.767604 0.782858 0.585777
--
/* andy buckle */
Re: Solved: Naming variables using existing variables...., bleagos, 2011/02/28