help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: slow 'eval' function - maybe pointers would do?


From: Carlo de Falco
Subject: Re: slow 'eval' function - maybe pointers would do?
Date: Fri, 30 Oct 2009 14:19:01 +0100


On 30 Oct 2009, at 13:27, Guido Walter Pettinari wrote:

Hi!

Thank you for the answers, Judd & Carlo!

You can do what you ask by:

for ii = 1:N
names{ii}  = sprintf("ks_%d", ii);
values{ii} = somehow_compute_value (ii);
endfor

S = cell2struct (values, names, 2)


You are right, but the point is that in my code each 'ks_i' is a
vector of M elements. I am filling all the 'ks_i' vectors inside a
'for i=1:M; for i=1:N' nested loop. If I were to call 'cell2struct' in
each sub-loop, I will obtain many structs, while my idea is to store
the data in a single struct. I want to obtain a struct 's' such that
each field 's.ks_i' contains a Mx1 vector (where M is big).

You can still get rid of the eval doing something like the following:

for ii =1:N
names{ii}  = sprintf("ks_%d", ii);
endfor

for jj=1:M
  for ii = 1:N
    KS{ii}(jj) = somehow_compute_value (ii,jj);
  endfor
endfor

S = cell2struct (KS, names, 2)

but still I don't understand why you want to call your vectors S.ks_ii,
I'm really curious: what's the problem with calling them KS{ii}?

By the way, I solved the issue by memorizing the 'ks_i' into a MxN
vector called 'ks' inside the loop. Once outside the loop, I filled
the struct in this way:

for j=1:N
  eval(  [ 's.k', int2str(j), ' = ks ( :, j );' ]     );
endfor

In this way I use more memory but the execution is faster.

I expect the option I suggest above to be faster, especially if N is big.

Thank you,
Guido

P.S. Carlo, is there any reason whereby in your example you used
sprintf instead of the string concatenation operator [ 'ks_', ii ] ?
Is the former faster?

I don't know about performance, I just find my syntax more elegant.

c.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]