5.1 控制流
5.1.1 条件控制——if,else,switch 下面我们通过三个简单的例子来说明这三个函数的使用。
l if语句的使用
- % Generate a random number
- a = randi(100, 1);
-
- % If it is even, divide by 2
- if rem(a, 2) == 0
- disp('a is even')
- b = a/2;
- end
命令窗口输出结果如下:
>>Untitled2
a is even
l else语句的使用
- a = randi(100, 1);
-
- if a < 30
- disp('small')
- elseif a < 80
- disp('medium')
- else
- disp('large')
- end
命令窗口输出结果如下:
>> Untitled2
large
l switch语句的使用
- [dayNum, dayString] = weekday(date, 'long', 'en_US');
-
- switch dayString
- case 'Monday'
- disp('Start of the work week')
- case 'Tuesday'
- disp('Day 2')
- case 'Wednesday'
- disp('Day 3')
- case 'Thursday'
- disp('Day 4')
- case 'Friday'
- disp('Last day of the work week')
- otherwise
- disp('Weekend!')
- end
命令窗口输出结果如下:
>> Untitled2
Weekend!
在这里顺便介绍一个类似于C语言中scanf的函数input并配合上面的if else实现一个小功能:
- yourNumber = input('Enter a number: ');
-
- if yourNumber < 0
- disp('Negative')
- elseif yourNumber > 0
- disp('Positive')
- else
- disp('Zero')
- end
运行上面代码后,我们在命令窗口输入数字22,输出结果如下:
>> Untitled2
Enter a number:22
Positive
5.1.2 循环控制——for, while, continue, break 这里我们也通过几个简单的例子来说明这几个函数的使用。
l for语句的使用
- for n = 3:32
- r(n) = rank(magic(n));
- end
- r
命令窗口输出结果如下:
>> Untitled2
r =
Columns 1 through 19
0 0 3 3 5 5 7 3 9 7 11 3 13 9 15 3 17 11 19
Columns 20 through 32
3 21 13 23 3 25 15 27 3 29 17 31 3
l while语句的使用
- a = 0; fa = -Inf;
- b = 3; fb = Inf;
- while b-a > eps*b
- x = (a+b)/2;
- fx = x^3-2*x-5;
- if sign(fx) == sign(fa)
- a = x; fa = fx;
- else
- b = x; fb = fx;
- end
- end
- x
命令窗口输出结果如下:
>> Untitled2
x =
2.0946
l continue语句的使用
- fid = fopen('magic.m','r');
- count = 0;
- while ~feof(fid)
- line = fgetl(fid);
- if isempty(line) || strncmp(line,'%',1) || ~ischar(line)
- continue
- end
- count = count + 1;
- end
- fprintf('%d linesn',count);
- fclose(fid);
命令窗口的输出结果如下:
>> Untitled2
31 lines
l break语句的使用
- a = 0; fa = -Inf;
- b = 3; fb = Inf;
- while b-a > eps*b
- x = (a+b)/2;
- fx = x^3-2*x-5;
- if fx == 0
- break
- elseif sign(fx) == sign(fa)
- a = x; fa = fx;
- else
- b = x; fb = fx;
- end
- end
- x
命令窗口输出结果如下:
>> Untitled2
x =
2.0946
5.1.3 矢量化 对于matlab而言,要想加快算法的执行速度可以通过算法的矢量化来实现,比如要实现如下的功能。
x = .01;
for k = 1:1001
y(k) = log10(x);
x = x + .01;
end
但是我们矢量化后,将更加方便和容易实现。
x = .01:.01:10;
y = log10(x);
但是有一点大家要特别注意,不是什么程序矢量化都能加快执行速度,要视具体情况而定。
5.1 控制流
5.1.1 条件控制——if,else,switch 下面我们通过三个简单的例子来说明这三个函数的使用。
l if语句的使用
- % Generate a random number
- a = randi(100, 1);
-
- % If it is even, divide by 2
- if rem(a, 2) == 0
- disp('a is even')
- b = a/2;
- end
命令窗口输出结果如下:
>>Untitled2
a is even
l else语句的使用
- a = randi(100, 1);
-
- if a < 30
- disp('small')
- elseif a < 80
- disp('medium')
- else
- disp('large')
- end
命令窗口输出结果如下:
>> Untitled2
large
l switch语句的使用
- [dayNum, dayString] = weekday(date, 'long', 'en_US');
-
- switch dayString
- case 'Monday'
- disp('Start of the work week')
- case 'Tuesday'
- disp('Day 2')
- case 'Wednesday'
- disp('Day 3')
- case 'Thursday'
- disp('Day 4')
- case 'Friday'
- disp('Last day of the work week')
- otherwise
- disp('Weekend!')
- end
命令窗口输出结果如下:
>> Untitled2
Weekend!
在这里顺便介绍一个类似于C语言中scanf的函数input并配合上面的if else实现一个小功能:
- yourNumber = input('Enter a number: ');
-
- if yourNumber < 0
- disp('Negative')
- elseif yourNumber > 0
- disp('Positive')
- else
- disp('Zero')
- end
运行上面代码后,我们在命令窗口输入数字22,输出结果如下:
>> Untitled2
Enter a number:22
Positive
5.1.2 循环控制——for, while, continue, break 这里我们也通过几个简单的例子来说明这几个函数的使用。
l for语句的使用
- for n = 3:32
- r(n) = rank(magic(n));
- end
- r
命令窗口输出结果如下:
>> Untitled2
r =
Columns 1 through 19
0 0 3 3 5 5 7 3 9 7 11 3 13 9 15 3 17 11 19
Columns 20 through 32
3 21 13 23 3 25 15 27 3 29 17 31 3
l while语句的使用
- a = 0; fa = -Inf;
- b = 3; fb = Inf;
- while b-a > eps*b
- x = (a+b)/2;
- fx = x^3-2*x-5;
- if sign(fx) == sign(fa)
- a = x; fa = fx;
- else
- b = x; fb = fx;
- end
- end
- x
命令窗口输出结果如下:
>> Untitled2
x =
2.0946
l continue语句的使用
- fid = fopen('magic.m','r');
- count = 0;
- while ~feof(fid)
- line = fgetl(fid);
- if isempty(line) || strncmp(line,'%',1) || ~ischar(line)
- continue
- end
- count = count + 1;
- end
- fprintf('%d linesn',count);
- fclose(fid);
命令窗口的输出结果如下:
>> Untitled2
31 lines
l break语句的使用
- a = 0; fa = -Inf;
- b = 3; fb = Inf;
- while b-a > eps*b
- x = (a+b)/2;
- fx = x^3-2*x-5;
- if fx == 0
- break
- elseif sign(fx) == sign(fa)
- a = x; fa = fx;
- else
- b = x; fb = fx;
- end
- end
- x
命令窗口输出结果如下:
>> Untitled2
x =
2.0946
5.1.3 矢量化 对于matlab而言,要想加快算法的执行速度可以通过算法的矢量化来实现,比如要实现如下的功能。
x = .01;
for k = 1:1001
y(k) = log10(x);
x = x + .01;
end
但是我们矢量化后,将更加方便和容易实现。
x = .01:.01:10;
y = log10(x);
但是有一点大家要特别注意,不是什么程序矢量化都能加快执行速度,要视具体情况而定。
举报