Question

Hi I have the following cellfun operation:

b = cellfun(@(x) nansum(cross(u{1},x)),r,'UniformOutput',false);

where u and r are vectors. I want to multiply the result of the cross product by a scalar called I, but it has to inside the summation. could I simply do:

b = cellfun(@(x) nansum(I.*cross(u{1},x)),r,'UniformOutput',false);

Would this work?

Actually just tried it and getting:

Matrix dimensions must agree.

Error in ==> cellcross>@(x)nansum(mag.*cross(u{1},x)) at 2
    b = cellfun(@(x) nansum(mag.*cross(u{1},x)),r,'UniformOutput',false);

Error in ==> cellcross at 2
    b = cellfun(@(x) nansum(mag.*cross(u{1},x)),r,'UniformOutput',false);

any suggestions?

update:

b = cellfun(@(x) nansum(bsxfun(@times, mag(:),cross(u{1},x))),r,'UniformOutput',false);
Was it helpful?

Solution

OK, based on the long discussion below, I now have a grasp on your problem. For future reference: providing a bit more context/detail can prevent such discussions.

Anyway, try this:

b = cellfun(@(x) nansum(...
    mag.* subsref(cross(u{1},x), struct('type', '()', 'subs', {':',':',3})) ),...
    r, 'UniformOutput',false);

Not the prettiest solution, but it does what you ask.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top