How do I create a for loop in MATLAB?
显示 更早的评论
I am completely lost in for loops, I just don't get it. The book and my professor haven't helped much. Where can I get help?
2 个评论
Abderrahmane Walid AISSANI
2017-5-14
编辑:Walter Roberson
2017-5-14
hi I am trying to create several variables using the for loop
% for k=1:1:10
tab_k = csvread('');
end
in fact the code is wrong but I want to iterate the "k" in the name of the variable but I can't find how to do that .
thank you .
采纳的回答
更多回答(10 个)
Jan
2012-3-5
You can get help from the documentation of Matlab:
doc for
help for
There you find examples and explanations.
1 个评论
Meenakshi Bhardwaj
2018-7-19
Thanks so much. Finally, I understood what is for loop.
Jan Afridi
2017-9-29
For loop repeat itself for a given number of input. The syntax for “For Loop Matlab” is
for variable = expression
Program Statement
end
In the above syntax, the expression has one of the following forms.
Initial value : Final value
for x = 1:10
fprintf('value of x: %d\n', x);
end
Initial value : Step : Final value
for x = 1:2:10
fprintf('value of x: %d\n', x);
end
Value Array
for x = [1 4 6 8 90]
disp(x)
end
for x = 1:10
fprintf('value of x: %d\n', x);
end
There are 4 type of loops: while, for, if and case.
For loop :
Eg: you have your robot whom you wish to give command to walk 100steps. The command will be
for steps=1:100
end
disp(steps)
The robot will go 100steps and stop and output will be displayed as 100 after completion.
For loop is used to solve many mathematical problems like factorials etc.
1 个评论
Walter Roberson
2023-1-11
Computer Scientists use the term "control statement" for code structures that will execute selectively exactly zero or one time. Computer Scientists use the term "loop" for code structures that have the potential to execute more than one time. "if" and "case" are control structures but are not loops.
If you imagine the execution point as starting from the "top" and falling downward, then "if" and "case" only ever have the execution point continuing to fall downwards, whereas "for" and "while" in general require pumping the execution point back up again.
Narasimman P
2021-7-30
for a=1:10
end
2 个评论
Ealam Yassin
2021-11-17
no output
The code posted by @Narasimman P is a completely valid for loop, just one that does not do anything inside the loop. All it does is count from 1 to 10 internally. After the loop, two things will have changed:
- Time will have elapsed, which could be important if you are waiting for something to happen
- The loop control variable 'a' will have the same value as it was last assigned, so in this case after the loop 'a' will have the double precision value 10 .
disp('before')
whos
disp('starting loop')
for a=1:10
end
disp('after')
whos
So there has been output: the variable a did not exist before, and after the loop it does exist.
Manan Shah
2022-5-8
编辑:Torsten
2022-5-8
for i = 0:8 ;
a = pow10 (i);
disp a(i);
end
2 个评论
Walter Roberson
2022-5-8
disp a(i)
would mean the same thing as
disp('a(i)')
You probably want
disp(a(i))
DGM
2023-11-4
Using
disp(a(i))
would necessitate addressing the zeroth and other nonexistent elements of the scalar variable a.
Besides the fact that this is obviously untested nonworking code, I question why what should rightly be a very simple example needs to include undisclosed user-defined functions.
D=input ('Βαθος νερου σε m ')
W=input ('Βαρος ανα μοναδα μηκους της γραμμης αγκυρωσης στο νερο σε N/m ')
Hex=input ('εξωτερικη φορτηση σε kn ')
dx=input ('οριζοντια μετατοπιση σε m ')
if dx/D>=0.3 && dx/D<=0.6
else
disp ('Δωσε διαφορετικη τιμη για το dx')
dx=input ('οριζοντια μετατοπιση σε m ')
end
I want to make my programm go to if after else and run that lines again until if line is satisfied
1 个评论
Walter Roberson
2022-11-15
while ~isnumeric(dx) || ~isscalar(dx) || dx/D<0.3 || dx/D>0.6
disp ('Δωσε διαφορετικη τιμη για το dx')
dx=input ('οριζοντια μετατοπιση σε m ')
end
Thanh
2023-11-4
0 个投票
I am using matlap 2017b but I no find simulation pacing options . I need some help .
1 个评论
Walter Roberson
2023-11-4
Introduced in R2018a
Sajidah
2024-5-8
编辑:Walter Roberson
2024-12-12
for x=1:5
disp(x);
end
hawra
2024-12-12
编辑:Walter Roberson
2024-12-12
clc
clear all
close all
s=0
n=1
for 1:0.5:100
s=s+n;
end
disp (s)
1 个评论
Walter Roberson
2024-12-12
You are missing a variable name and "=" in the "for" loop.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!