mathneuro
2009-01-29 03:04:23 UTC
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.
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.