Borrar filtros
Borrar filtros

How to input a matrix of unknown dimensions into a function?

5 visualizaciones (últimos 30 días)
function avg_vals = function_name(A)
A = %This is where I'm confused
avg_vals = mean(A,2);
end
I'm trying to create a function that will accept a matrix (A) that contains an unspecified number of rows and columns. I want it to then average the rows (rather than columns) and return those values as a column vector. It could be that I'm approaching this from the totally wrong angle, so any advice is welcome.

Respuesta aceptada

Ahmed A. Selman
Ahmed A. Selman el 10 de Abr. de 2013
A is an input argument passed from function call function_name(A). SO, DELETE THIS LINE from the beginning, you do not need to input A a second time :)
function avg_vals = function_name(A)
A = % and this is where I am truly confused. Delete this line
avg_vals = mean(A,2);
end
When you want to work with this function, use:
ex1.:
A=1:10; % This is a 1-D vector
function_name(A)
ex2.
A=magic(4); % This is a 2-D matrix
function_name(A)
  3 comentarios
James Tursa
James Tursa el 11 de Abr. de 2013
A matrix is entered using the square brackets. So try this instead:
function_name([1 2 3; 4 5 6; 7 8 9])
Everything inside the [...] is treated as a single matrix.
Leah
Leah el 11 de Abr. de 2013
That did the trick!
Thank you for your help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by