Matlab equivalent of Python's 'reduce' function -
i have bunch of matrices of same size m*n: a, b, c, d, , i'd find maximum of them elementwise, like:
mx = max(a, max(b, max(c, d))); apparently code above not concise enough, i've googled , didn't find max on n matrices, or matlab function python's reduce. haven't learned matlab, there one?
make n*m*4 matrix of input, can use max:
m=cat(3,a,b,c,d) max(m,[],3) the cat parameter 3 concatenates matrices along third dimension, , max finds maximum along dimension. compatible arbitrary matrix dimensions:
d=ndims(a) m=cat(d+1,a,b,c,d) max(m,[],d+1) reduce not exist, , typically don't need because multi dimensional inputs or varargin trick, if need it, it's simple implement:
function r=reduce(f,varargin) %example reduce(@max,2,3,4,5) while numel(varargin)>1 varargin{end-1}=f(varargin{end-1},varargin{end}); varargin(end)=[]; end r=varargin{1}; end
Comments
Post a Comment