MATLAB for循环语句

本文概述

  • 句法
  • 例1
  • 例2
  • 范例3
for循环用于循环语句特定次数。并且还使用递增或递减的索引变量来跟踪每次迭代。
句法
for index = values < program statements> ... end

例1
% program to print multiples of first prime number between 1000 and 2000% using for looppr = 0; for k = 1000:2000if isprime(k)pr = k; disp(['The first prime number is : ', num2str(pr)])for m = pr:pr:pr*10disp(m)endbreakendend

输出
The first prime number is: 100910092018302740365045605470638072908110090

例2
for a = 10:20 fprintf('value of a: %d\n', a); end

输出
value of a: 10value of a: 11value of a: 12value of a: 13value of a: 14value of a: 15value of a: 16value of a: 17value of a: 18value of a: 19value of a: 20

范例3
for a = 1.0: -0.1: 0.0 disp(a)end

【MATLAB for循环语句】输出
19/104/57/103/51/22/53/101/51/100

    推荐阅读