Matlab equivalent of Python's 'reduce' function

zhangxaochen

I have a bunch of matrices of the same size m*n: a, b, c, d, and I'd like to find the maximum of them elementwise, like:

mx = max(a, max(b, max(c, d)));

apparently the code above is not concise enough, I've googled and didn't find much help about max on N matrices, or any matlab function like python's reduce. I haven't learned much about matlab, so is there one?

Daniel

Make a n*m*4 matrix of your input, then you can use max:

M=cat(3,a,b,c,d)
max(M,[],3)

The cat with parameter 3 concatenates your matrices along the third dimension, and max finds the maximum along this dimension. To be compatible with arbitrary matrix dimensions:

d=ndims(a)
M=cat(d+1,a,b,c,d)
max(M,[],d+1)

Reduce itself does not exist, and typically you don't need it because multi dimensional inputs or varargin do the trick, but if you need it, it's simple to 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Matlab equivalent of Python's 'reduce' function

From Dev

What is the Java equivalent to Python's reduce function?

From Dev

Is there a Python equivalent to MATLAB's pearsrnd function?

From Dev

Python equivalent to Matlab's set function

From Dev

What is the equivalent of matlab's smooth3 function in python?

From Dev

The equivalent function of Matlab imfilter in Python

From Dev

Matlab importdata() function equivalent in Python

From Dev

Equivalent matlab damp function for Python

From Dev

Python equivalent of MATLAB function findchangepts

From Dev

What is the MATLAB equivalent of Excel's NORMSDIST function?

From Dev

Is there an equivalent to matlab's rcond() function in Julia?

From Dev

Torch tensor equivalent function to matlab's "find"?

From Dev

What is the MATLAB equivalent of Excel's NORMSDIST function?

From Dev

What is python's equivalent of Matlab's ranksum?

From Dev

Equivalent function of datenum(datestring) of Matlab in Python

From Dev

Python - equivalent of Matlab/Octave keyboard function?

From Dev

Matlab equivalent of Python exit function for debugging purposes

From Dev

Matlab's Logicals for Array Indexing - Python Equivalent

From Dev

Is there a Python equivalent to Matlab's makelut, applylut?

From Dev

Is there an equivalent of Python's list and append feature in Matlab?

From Dev

Matlab equivalent of Python's "pass" statement

From Dev

What is the equivalent of matlab's wkeep in Python?

From Dev

What are Python's equivalent of Javascript's reduce(), map(), and filter()?

From Dev

Python's equivalent for R's dput() function

From Dev

Equivalent R function for Matlab

From Dev

Equivalent R function for Matlab

From Dev

Equivalent function of Matlab's filter2 in OpenCV

From Dev

Is there any function equivalent to Matlab's imadjust in OpenCV with C++?

From Dev

Is there any function equivalent to Matlab's imadjust in OpenCV with C++?

Related Related

  1. 1

    Matlab equivalent of Python's 'reduce' function

  2. 2

    What is the Java equivalent to Python's reduce function?

  3. 3

    Is there a Python equivalent to MATLAB's pearsrnd function?

  4. 4

    Python equivalent to Matlab's set function

  5. 5

    What is the equivalent of matlab's smooth3 function in python?

  6. 6

    The equivalent function of Matlab imfilter in Python

  7. 7

    Matlab importdata() function equivalent in Python

  8. 8

    Equivalent matlab damp function for Python

  9. 9

    Python equivalent of MATLAB function findchangepts

  10. 10

    What is the MATLAB equivalent of Excel's NORMSDIST function?

  11. 11

    Is there an equivalent to matlab's rcond() function in Julia?

  12. 12

    Torch tensor equivalent function to matlab's "find"?

  13. 13

    What is the MATLAB equivalent of Excel's NORMSDIST function?

  14. 14

    What is python's equivalent of Matlab's ranksum?

  15. 15

    Equivalent function of datenum(datestring) of Matlab in Python

  16. 16

    Python - equivalent of Matlab/Octave keyboard function?

  17. 17

    Matlab equivalent of Python exit function for debugging purposes

  18. 18

    Matlab's Logicals for Array Indexing - Python Equivalent

  19. 19

    Is there a Python equivalent to Matlab's makelut, applylut?

  20. 20

    Is there an equivalent of Python's list and append feature in Matlab?

  21. 21

    Matlab equivalent of Python's "pass" statement

  22. 22

    What is the equivalent of matlab's wkeep in Python?

  23. 23

    What are Python's equivalent of Javascript's reduce(), map(), and filter()?

  24. 24

    Python's equivalent for R's dput() function

  25. 25

    Equivalent R function for Matlab

  26. 26

    Equivalent R function for Matlab

  27. 27

    Equivalent function of Matlab's filter2 in OpenCV

  28. 28

    Is there any function equivalent to Matlab's imadjust in OpenCV with C++?

  29. 29

    Is there any function equivalent to Matlab's imadjust in OpenCV with C++?

HotTag

Archive