Problem 2170. Dealfun (1.0)
Short description.
Write a function dealfun:
[y1,y2,...,yn]=dealfun(fhandle,x1,x2,...,xn)
which evaluates the function handle fhandle on arguments x1,x2,...,xn independently and deal solutions to outputs y1,y2,...,yn (so nargin==nargout+1)
Long description.
Several repetitions of exacly same operation on different variables can be a pain in the neck when you are editing your code.
a=a^2+mod(a,3); a=a^3+4*a+mod(a,3); b=b^2+mod(a,3); --> b=b^3+4*a+mod(a,3); c=c^2+mod(a,3); c=c^3+4*a+mod(a,3); d=d^2+mod(a,3); d=d^3+4*a+mod(a,3);
Of course there are as many strategies as programmers. There are also many helpful functions like cellfun arrayfun deal feval. But sometimes usage of those functions in one line looks unclear and it doesn't make editing faster. Usage of dealfun could look like that:
funh=@(A)A^2+mod(A,3); --> funh=@(A)A^3+4*A+mod(A,3); [a,b,c,d]=dealfun(@funh,a,b,c,d);
example:
>> [a,b,c,d]=dealfun(@sum,ones(1),ones(2),ones(3),ones(4)) a = 1 b = 2 2 c = 3 3 3 d = 4 4 4 4
easy change:
>> [a,b,c,d]=dealfun(@numel,ones(1),ones(2),ones(3),ones(4)) a = 1 b = 4 c = 9 d = 16
Solution Stats
Problem Comments
-
2 Comments
I think you try to get the "i" and "j" that the function nonzeros does not give:
http://www.mathworks.de/de/help/matlab/ref/nonzeros.html
Thanks José, I've fixed bugs.
Solution Comments
Show commentsProblem Recent Solvers28
Suggested Problems
-
Return a list sorted by number of occurrences
2822 Solvers
-
Number of 1s in the Binary Representation of a Number
457 Solvers
-
Back to basics 25 - Valid variable names
328 Solvers
-
Find the maximum number of decimal places in a set of numbers
3015 Solvers
-
Set the array elements whose value is 13 to 0
1381 Solvers
More from this Author40
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!