MATLAB Answers

0

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);
end
Any other/proper/quick way to do this? (Note: length of all X.Y is not same.)

  0 Comments

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)

  4 Comments

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
Yep!

Sign in to comment.