matlab - Is there a version of bsxfun that works on structure arrays? -
why can this:
a = [1 2]; b = [3 4]; bsxfun(@(ai,bj) ai + bj, a, b') % 4 5 % 5 6
but not this:
a = struct('x', {1 2}); b = struct('x', {3 4}); bsxfun(@(ai,bj) ai.x + bj.x, a, b'); % error using bsxfun % operands must numeric arrays.
and replacement function exist works in both cases?
this may not general solution* particular example easy convert structure array numerical array inside bsxfun, using comma-separated-list generator syntax, , using original anonymous function, i.e.
>> bsxfun(@(ai, bj) ai+bj, [a.x], [b.x]') ans = 4 5 5 6
and should still leverage computational efficiency conferred bsxfun
(as opposed slower "repmat
+arrayfun
" approach, instance).
*e.g. might not work intended if field contains array instead of scalar, since expansion comma-separated-list different
Comments
Post a Comment