In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to achieve for cycle in matlab". In daily operation, I believe many people have doubts about how to achieve for cycle in matlab. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to achieve for cycle in matlab"! Next, please follow the editor to study!
Simple for cycle
The for loop is used to loop through the data.
Example: output the sum of 1x 100
> > clear > > sum = 0; > for I = 1for 100 sum = sum + I; end > > sumsum = 5050
Explanation: I from 1 to 100, increase one at a time, the total cycle is 100 times
Note: the location of the semicolon; the "+ =" symbol cannot be used; each layer of loop must end with end
For loop with step size
Example: output odd numbers and sums between 1 and 10
> > clear > > sum = 0; > for I = 1:2:10 sum = sum + I; end > > sumsum = 25
Explanation: the 2 in the middle of I = 1:2:10 represents the step size, which means that from 1 to 10, increase by 2 each time, that is, the sum of five numbers, that is, 1, 3, 5, 5, 7, 9; the step size can also be negative.
Traversal of vectors and matrices traversal of vectors
> > clear > > A = rand (1Power4); > > for I = An i endi = 0.9572i = 0.4854I = 0.8003I = 0.1419
Explanation: the rand function is used to randomly generate numbers between 0 and 1, and rand (1 ~ 4) generates column vectors of 1 ~ 4.
Traversal of matrix > > A = magic (4); > > AA = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 > > for I = A; i endi = 16 59 4 I = 2 11 7 14 I = 3 10 6 15 I = 13 8 12 1
Explanation: the magic (n) function is used to generate a square matrix with any row or column or the sum of primary and secondary diagonals equal. The cyclic traversal of the matrix is to take out each column element of the matrix in turn.
The use of break and continue
Break is used to terminate the nearest layer of for loop
Continue is used to skip the nearest for loop and then execute the next loop
> > x = 1; > > for I = 1:2:10 if I > 7 break else x = x * i end endx = 1x = 3x = 15x = 105 > > sum = 0; > > for I = 1:6 if I = = 4 continue else sum = sum + I end end;sum = 1sum = 3sum = 6sum = 11sum = 17 multi-layer for cycle
For loops can be nested as needed.
> > for I = 1:2:6 for j = 1:3 y = I * j end endy = 1y = 2y = 3y = 3y = 6y = 9y = 5y = 10y = 15 this is the end of the study on "how to implement the for cycle in matlab". I hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.