Cody

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

Solution 1810124

Submitted on 8 May 2019
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   Fail
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))

Out of memory. The likely cause is an infinite recursion within the program. Error in structfun2 (line 11) assert(isequal(structfun2(f,s),c))

2   Fail
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))

Scalar structure required for this assignment. Error in structfun2 (line 2) s.f1 = 'Sunday'; Error in Test2 (line 4) assert(isequal(structfun2(f,s),c))

3   Fail
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))

Scalar structure required for this assignment. Error in structfun2 (line 2) s.f1 = 'Sunday'; Error in Test3 (line 4) assert(isequal(structfun2(f,s),c))

4   Fail
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))

Scalar structure required for this assignment. Error in structfun2 (line 2) s.f1 = 'Sunday'; Error in Test4 (line 4) assert(isequal(structfun2(f,s),c))