How to calculate the average between each two adjacent members(rows or columns)?

84 visualizaciones (últimos 30 días)
Hi All
I have an array like
a= [1 2 3 4 5]
and I need the average between two members and add it to a new array
av= [1.5, 2.5, 3.5, 4.5]
which will have one les member obviously

Respuesta aceptada

Tommy
Tommy el 28 de Abr. de 2020
Try this:
>> a= [1 2 3 4 5];
b = (a(1:end-1) + a(2:end))/2
b =
1.5000 2.5000 3.5000 4.5000
  1 comentario
Deepak Gupta
Deepak Gupta el 28 de Abr. de 2020
other methods:
1.
a= [1 2 3 4 5];
b = mean([a(1:end-1); a(2:end)]);
2.
a= [1 2 3 4 5];
b = 0.5 * (a(1:end-1) + a(2:end));
3.
a= [1 2 3 4 5];
temp = 0.5*conv([1 1], a);
b = temp(2:end-1);

Iniciar sesión para comentar.

Más respuestas (1)

Saurav Roy
Saurav Roy el 28 de Abr. de 2020
Hey,
Try this for the Row and adjust accordingly for the columns.
arr = [1 2 3 4 5];
len = length(arr);
arr_secondary = [];
for i = 1:len-1
primarynum = arr(i);
secondarynum = arr(i+1);
avgnum = (primarynum + secondarynum)/2;
arr_secondary(i) = avgnum;
end
disp(arr_secondary);

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by