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.)

 Accepted Answer

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

4 Comments

Thanks, learned a new way. Please change 'x' to 'X' to comply with the question.
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?
Yep!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!