How do I find the average of a vector of units by using a for loop?

32 次查看(过去 30 天)
This is what I've got so far. I'm trying to find the average of the vector H values.
for i = 1 : 1 : length (H);
if i <= 22
symsum = i + H(i);
end
disp (i);
fprintf(1, '%d\n', i)
end

回答(1 个)

John D'Errico
John D'Errico 2025-9-28,22:37
编辑:John D'Errico 2025-9-29,12:50
When you create a variable name, NEVER, NEVER, NEVER use a name that is an existing and useful function name. (Well, it might be useful to you one day.) In this case, it is the name symsum.
help symsum
--- help for sym/symsum --- sym/symsum - Symbolic sum of series This MATLAB function returns the symbolic definite sum of the series f with respect to the summation index k from the lower bound a to the upper bound b. Syntax F = symsum(f,k,a,b) F = symsum(f,k) Input Arguments f - Expression defining terms of series symbolic expression | symbolic function | symbolic vector | symbolic matrix | symbolic number k - Summation index symbolic variable a - Lower bound of summation index number | symbolic number | symbolic variable | symbolic expression | symbolic function b - Upper bound of summation index number | symbolic number | symbolic variable | symbolic expression | symbolic function Examples openExample('symbolic/FindSumOfSequenceOfNumbersExample') openExample('symbolic/FindSumOfSeriesWithBoundsExample') openExample('symbolic/FindIndefiniteSumOfSeriesExample') openExample('symbolic/SummationOfPolynomialSeriesExample') See also cumsum, int, sum, symprod, syms, symvar, rewrite Introduced in Symbolic Math Toolbox before R2006a Documentation for sym/symsum doc sym/symsum
One day you will want to use the function symsum, and then you will return to this forum, asking why symsum does not work correctly for you. (You will also get a confusing error message, because MATLAB is also confused at that point.)
You might also get into the good habit of not using i as an index counter variable. as i is already defined in MATLAB as sqrt(-1). However, I'll be honest that growing up with a Fortran background, I still use i for an index myself. Some old habits die hard. And this means it is best if you start out with good habits from the very beginning.
As far as what you did, you want to compute an average. What is the formula for an average? You take the sum, and then divide by the number of elements in that sum. So you need to, at the end, divide by length(H).
There is no need to use an if however, and I don't have a clue why you are adding i to the sum in that sum.
% Pre-define Hsum to start as zero before the loop.
Hsum = sym(0);
for i = 1 : length(H);
Hsum = Hsum + H(i); % Accumulate elements of H into the sum
end
Havg = Hsum/length(H); % converting the sum into the average.
Note that I initialized Hsum as zero. This way, in case you ever used the name Hsum before, you want it to start out as zero. It is just good programming practice to initialize variables like a sum. Make it a habit, and you will be happy forever after. Well, maybe that is a bit too much to hope for, but it is a start. ;-)
Finally, can you write that average even more simply, with no need for a loop at all? Of course. Consider this alternative:
Havg = sum(H)/length(H); % compute the average of the elements in vector H
That line of code has several virtues, as is it completely eliminates any need for a loop. It is quite easy to read, and that makes your code easier to debug, and faster to write.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by