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

53 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Tommy
Tommy il 28 Apr 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 Commento
Deepak Gupta
Deepak Gupta il 28 Apr 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);

Accedi per commentare.

Più risposte (1)

Saurav Roy
Saurav Roy il 28 Apr 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);

Categorie

Scopri di più su Data Types in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by