フィルターのクリア

Using while loop to determine average

10 ビュー (過去 30 日間)
student187
student187 2015 年 10 月 18 日
コメント済み: Khalid Ghwani 2021 年 4 月 3 日
I need to calculate the average using a while loop only. The first step is to add up all my vectors into one number. This is what I've done so far.
fid = fopen('Watts.dat');
r1 = fscanf(fid,'%f');
stop = 0;
sumx = 0;
n = 1;
while (stop <= 31)
f = r1(n)
sumx = r1(f) + sumx;
stop = stop +1;
n = n+1
end
However, I get the message
Attempted to access r1(2005.6); index must be a positive integer or logical.
I can't round these numbers. Do you guys have any suggestions?
  1 件のコメント
Khalid Ghwani
Khalid Ghwani 2021 年 4 月 3 日
Use a while loop to write a Matlab program to find the average any set of numbers entered by the user (not of known size)

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

採用された回答

Jan
Jan 2015 年 10 月 18 日
編集済み: Jan 2015 年 10 月 18 日
Replace
sumx = r1(f) + sumx;
by
sumx = f + sumx;
Note: You do not need two variables "n" and "stop". One of them is enough.
Do not forget to close opened files with fclose.
  1 件のコメント
student187
student187 2015 年 10 月 18 日
Thank you for your time, however, don't I need the n to push matlab to find the next data point in the vector?

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

その他の回答 (0 件)

カテゴリ

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