Add to a vector from loop
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 6 Mär. 2021
Start here to learn the answer to your question and also fix several other bugs and features of your code:
Matt Baron
am 6 Mär. 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
am 6 Mär. 2021
Matt Baron
am 27 Mär. 2021
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Function Creation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!