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

62 ビュー (過去 30 日間)
farzad
farzad 2020 年 4 月 28 日
コメント済み: farzad 2020 年 4 月 28 日
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

採用された回答

Tommy
Tommy 2020 年 4 月 28 日
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 件のコメント
Deepak Gupta
Deepak Gupta 2020 年 4 月 28 日
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);

サインインしてコメントする。

その他の回答 (1 件)

Saurav Roy
Saurav Roy 2020 年 4 月 28 日
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);

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by