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

53 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Tommy
Tommy am 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 Kommentar
Deepak Gupta
Deepak Gupta am 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);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Saurav Roy
Saurav Roy am 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);

Kategorien

Mehr zu Data Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by