MATLAB Answers


sum of array in structure.

Asked by Pramit Biswas on 3 Jan 2018
Latest activity Commented on by Elias Gule
on 16 Jan 2018
uC = zeros(1,length(X));
for loop1 = 1:length(X)
uC(loop1) = sum(X(loop1).Y);
Any other/proper/quick way to do this? (Note: length of all X.Y is not same.)


Sign in to comment.

1 Answer

Answer by Elias Gule
on 3 Jan 2018
 Accepted Answer

try this.
uC = arrayfun(@(v) sum(v.Y),x)


Show 1 older comment
Note that this will be slower than a loop with a preallocated output array.
oops!!! Is this function only to reduce code? Any other ways?
Elias Gule
on 16 Jan 2018

Sign in to comment.