MATLAB Answers

Find the sum of the vector using a for loop

4 views (last 30 days)
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.

  0 Comments

Sign in to comment.

Accepted Answer

Tyde Hilderbrandt
Tyde Hilderbrandt on 29 Mar 2020
The awnser ended up being

  0 Comments

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 25 Mar 2020
Edited: the cyclist on 25 Mar 2020
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 Comments

Show 2 older comments
Tyde Hilderbrandt
Tyde Hilderbrandt on 25 Mar 2020
Thank you for the link, but I dont think this will work since it is a homework problem which requires the use of "for". I would ask my professor for help but with the current status of universitys, office hours are no long avalible until new scheduling is figured out and spring break is over, March 30. I am going to continue to try and solve it my self but the help is very much appriaciated.
the cyclist
the cyclist on 25 Mar 2020
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 on 25 Mar 2020
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

Sign in to comment.

Sign in to answer this question.

Tags


Translated by