To operate a function in all rows of matrix

How I can repeat a function irrespective a size of matrix? For example, I have a matrix A and I want calculate the difference between maximum and minimum values in all rows, without I have determine the number of rows in my function, because I have began to set the function, but I just get the function works writtening a number of each row. Thank you!
A =
66 94 75 18
4 68 40 71
85 76 66 4

 Respuesta aceptada

Adam Danz
Adam Danz el 8 de Feb. de 2019
Editada: Adam Danz el 8 de Feb. de 2019
The range() function computes the difference between max and min values. When the input is a matrix, it acts on the columns. To act on the rows, just transpose the matrx. I added a second transpose so the results are in a column.
maxMinDiff = range(A')'
maxMinDiff =
76
67
81

3 comentarios

Steven Lord
Steven Lord el 8 de Feb. de 2019
Rather than transposing the matrix, I recommend specifying the dimension over which to operate.
The range function is part of Statistics and Machine Learning Toolbox.
If you don't have Statistics and Machine Learning Toolbox, use the bounds function or the min and max functions (all three of which are in MATLAB and all three of which also accept a dimension over which to operate) and subtract the results.
Oh yeah, that's better.
maxMinDiff = range(A, 2)
maxMinDiff =
76
67
81
%or
maxMinDiff = max(A,[],2) - min(A,[],2)
maxMinDiff =
76
67
81
Rafael Zanetti
Rafael Zanetti el 8 de Feb. de 2019
I thank you, work it, I was breaking the head researching and I was not achieving, one more time, I am grateful.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Versión

R2017b

Preguntada:

el 8 de Feb. de 2019

Comentada:

el 8 de Feb. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by