フィルターのクリア

How can I calculate average of two values using a for loop

23 ビュー (過去 30 日間)
collegestudent
collegestudent 2020 年 4 月 6 日
コメント済み: collegestudent 2020 年 4 月 6 日
I have to calculate the average of two numbers in different columns but are in the same row. This then has to be repeated for the other 20 rows of numbers I have finding the average between the two numbers.
for ii = 1:20
PTS = {539 1220 996 1485 1938 2019 2461 1557 1819 2832 2430 2323 2201 1970 2078 1616 2133 83 782 1161}
G = {71 79 50 66 68 80 82 65 66 80 77 82 82 73 82 58 78 6 35 66}
ave = PTS./G
end
  3 件のコメント
collegestudent
collegestudent 2020 年 4 月 6 日
Each number in the PTS cell array corresponds to the number in G so 539 divided by 71 to get the average and so on for the rest of the numbers
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 4 月 6 日
Is the question complete?

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

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 4 月 6 日
編集済み: KALYAN ACHARJYA 2020 年 4 月 6 日
Why you are dealing with cell array, you cal do same with array, By the way ave = PTS./G is not the average
PTS = {539 1220 996 1485 1938 2019 2461 1557 1819 2832 2430 2323 2201 1970 2078 1616 2133 83 782 1161}
G = {71 79 50 66 68 80 82 65 66 80 77 82 82 73 82 58 78 6 35 66}
for ii=1:length(PTS)
ave{ii} = PTS{ii}/G{ii}
end
Array
PTS =[539 1220 996 1485 1938 2019 2461 1557 1819 2832 2430 2323 2201 1970 2078 1616 2133 83 782 1161]
G = [71 79 50 66 68 80 82 65 66 80 77 82 82 73 82 58 78 6 35 66]
for ii=1:length(PTS)
ave(ii)= PTS(ii)/G(ii})
end
  1 件のコメント
collegestudent
collegestudent 2020 年 4 月 6 日
I am trying to calculate the average points per season using the equation average = points / games

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

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 4 月 6 日
This is a version without a for loop
ave = [PTS{:}]./[G{:}];

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by