Cody

# Problem 43685. Apply Function to Each Field of a Structure Array: Part 1

Solution 1061861

Submitted on 27 Nov 2016 by Peng Liu
• Size: 25
• This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
s.f1 = 'Sunday'; s.f2 = 'Monday'; s.f3 = 'Tuesday'; s.f4 = 'Wednesday'; s.f5 = 'Thursday'; s.f6 = 'Friday'; s.f7 = 'Saturday'; f = @numel; c = structfun(@numel, s); assert(isequal(structfun2(f,s),c))

2   Pass
s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]}); f = @(x)min(x(:)); c = {[1;1], [3;2]; [1;3], [5;2]}; assert(isequal(structfun2(f,s),c))

3   Pass
s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]}); f = @(x)max(x(:)); c = {[1;2], [4;2]; [1;5], [6;5]}; assert(isequal(structfun2(f,s),c))

4   Pass
s = struct('f1',{0, [3 4]; 1, [5 7 6]},'f2',{[0 0], 2; [2 4 5], [0 5]}); f = @nnz; c = {[0;0], [2;1]; [1;3], [3;1]}; assert(isequal(structfun2(f,s),c))