Find the sum of the vector using a for loop

5 ビュー (過去 30 日間)
Tyde Hilderbrandt
Tyde Hilderbrandt 2020 年 3 月 25 日
回答済み: Tyde Hilderbrandt 2020 年 3 月 29 日
I have been looking through the community for the awnser to this since it has been brought up so much, and it has helped me alot, but I'm missing something and dont know what keywords to use to find it.
I'm trying to sum the rows of my vector x but the current code does not seem to do this, I think it an error with n=50:1 but dont know what else todo.

採用された回答

Tyde Hilderbrandt
Tyde Hilderbrandt 2020 年 3 月 29 日
The awnser ended up being

その他の回答 (1 件)

the cyclist
the cyclist 2020 年 3 月 25 日
編集済み: the cyclist 2020 年 3 月 25 日
If you type the expression
50:1
into MATLAB, you will get the result
>> 50:1
ans =
1×0 empty double row vector
So, your for loop doesn't have any iterations.
You could try
for n = 1:50
instead.
Also, note that
sum(x(1:50))
will sum the first 50 elements of x, without a for loop, and
sum(x)
will sum all the elements of x.
Seems like you might benefit from a beginners tutorial in MATLAB. You might want to take a look at the MATLAB Onramp.
  5 件のコメント
the cyclist
the cyclist 2020 年 3 月 25 日
OK. I think there are still some elements of those solutions you could use. For example, you could still use the table2array function to convert the table to a numeric array, and then use a for loop for the sum.
Tyde Hilderbrandt
Tyde Hilderbrandt 2020 年 3 月 25 日
編集済み: Tyde Hilderbrandt 2020 年 3 月 25 日
Yes, I have saw that and added it to my code, defiently making progress. Here is what I have now. P.S changed z(n)=z+x to z(n)=n+x

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

カテゴリ

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