Discussion:
Vectorizing Assignment to an Array of Structures
(too old to reply)
mathneuro
2009-01-29 03:04:23 UTC
Permalink
Hello,

I am looking for a vectorized method for assigning elements to an
array of structures. For

x = 1:10;
N = length(x);

y = repmat(struct('field1',[],'field2',[]),1,N);

Suppose I want to do this:

for n = 1:N
y(n).field1 = x(n);
end

In MATLAB, I can simply do:

xcell = num2cell(x);
[y.field1] = xcell{:};

and avoid the loop. This is an extremely useful general tool for me in
MATLAB, with frequent speed gains from 2x - 4x depending on the size
of the loop, especially after caching the function. (Sometimes it's
slower, but that's another story.)

My question is: how can this be accomplished in Octave? I've had no
luck playing around with various methods of vectorized assignment.

Thanks.
Francesco Potortì
2009-01-29 09:10:12 UTC
Permalink
Post by mathneuro
xcell = num2cell(x);
[y.field1] = xcell{:};
Have you tried this in Octave? It works for me in 3.1.51.

Loading...