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

49 vues (au cours des 30 derniers jours)
farzad
farzad le 28 Avr 2020
Commenté : farzad le 28 Avr 2020
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

Réponse acceptée

Tommy
Tommy le 28 Avr 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 commentaire
Deepak Gupta
Deepak Gupta le 28 Avr 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);

Connectez-vous pour commenter.

Plus de réponses (1)

Saurav Roy
Saurav Roy le 28 Avr 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);

Catégories

En savoir plus sur Data Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by