Add to a vector from loop
Show older comments
Team,
Just learning how to use MatLab here,
How do I insert the value n into a different variable so I can recall each number? Any other advice is much appreciated. I just started MATH240 so will need more help later ;)
-----------------------------------------------
totaln=0;
n=1;
for n=1:1000000;
if abs(cos(n))<1/n;
totaln=totaln+1;
display(n) %this displays n but then updates it with the next iteration, I want to insert the value of n into a vector so I can see recall them later.
n=n+1;
else;
n=n+1;
end;
end;
5 Comments
Stephen23
on 6 Mar 2021
Start here to learn the answer to your question and also fix several other bugs and features of your code:
Matt Baron
on 6 Mar 2021
"which part of the tutorial has loop and vector information?"
But this might be more useful than those introductory pages:
Note that MATLAB usually works best when working with vectors/matrices/arrays. So rather than solving that task using a loop, the standard approach would be to generate a vector and use logical indexing to select the values that meet that condition:
"This is the first time I am using code and the amount of information is overwhelming."
I know that feeling! Read the documentation, experiment, read more documentation, don't worry about making mistakes :)
Matt Baron
on 6 Mar 2021
Matt Baron
on 27 Mar 2021
Accepted Answer
More Answers (0)
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!