Function output an array

38 visualizaciones (últimos 30 días)
Gary Niemeier
Gary Niemeier el 29 de Ag. de 2022
Editada: VBBV el 29 de Ag. de 2022
I am trying towrite a function that takes an array of variable size, and calculates the differneces between the 1,2 2,3 3,4 ... inputs and displays as an array (without using the diff function). It keep giving a singular output for the first values but need it to give an array for all. any help would be great.
function [out] = differences(in)
%This function allows an array input and the differences between the array
i=1;
while (i<=size(in))
out(i)= in(i+1)-in(i);
i=i+1;
end
end

Respuestas (1)

VBBV
VBBV el 29 de Ag. de 2022
Editada: VBBV el 29 de Ag. de 2022
in = [rand(1,15);rand(1,15)];
differences(in)
ans = 1×15
0.2886 -0.0528 0.2459 -0.6453 -0.3648 -0.6805 0.3954 0.1678 0.1278 -0.2929 -0.5549 -0.1945 -0.4947 -0.0121 0.1477
function [out] = differences(in)
%This function allows an array input and the differences between the array
i=1;
while (i<size(in,1))
out(i,:)= in(i+1,:)-in(i,:);
i=i+1;
end
end

Categorías

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

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by