纯小白零基础,非小白绕道,文档写的比较乱,将就着看吧。
一、for循环
for循环和c++的for循环还是有区别的,用起来简化很多。格式:for 。。。end
example1;
创建一个10阶的希尔伯特矩阵:
复制代码
数字求和:
1
2
3
4
5
6
7
8s = 10; H = zeros(s); for c = 1:s for r = 1:s H(r,c) = 1/(r+c-1); end end
复制代码
矩阵运算:
1
2
3
4
5sum=0; for i=1:1:100 sum=sum+i; end fprintf('sum =%d',sum);
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17a=[1 2 3;4 5 6;7 8 9]; for i=1:size(a,1)//矩阵a的行 for j=1:size(a,2)//矩阵a的列 if a(i,j)>5 a(i,j)=sqrt(a(i,j)); end end end for i=1:size(a,1) for j=1:size(a,2) if(j>2) fprintf('%fn',a(i,j)); else fprintf('%ftt',a(i,j)); end end end
result:
1.000000 2.000000 3.000000
4.000000 5.000000 2.449490
2.645751 2.828427 3.000000
二、while循环
1.用while循环计算10的阶乘:
复制代码
2.计算输入数(正数)的平均数:
1
2
3
4
5
6
7n = 10; f = n; while n > 1//判断条件不带括号也行啊 n = n-1; f = f*n; end disp(['n! = ' num2str(f)])
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21%变量如下: %x %sum_x,n,average x=input('input one number please !n'); sum_x=0; n=0; while(x>0) n=n+1; sum_x=sum_x+x; x=input('input one number please !n'); if (x<0) break; end; end; if(n==0) fprintf('input a positive number please !n'); else fprintf('Only input a number so only the average !n') average=sum_x/n; fprintf('average is :%fn',average); end
三、逻辑数组
就是给一个数组进行逻辑运算,它和数组用循环的方式计算效果一样,但效率比较高。
1.比如一个数组a=[1 2 3;4 5 6;7 8 9],对它执行命令c=a>3时,此时逻辑矩阵c=[0 0 0;1 1 1;1 1 1]; 再执行a(c)时,就是取a矩阵里对应逻辑矩阵c为真时的值:4 5 6 7 8 9;
若执行a(c)=sqrt(a(c))时,
a =
1.0000 2.0000 3.0000
2.0000 2.2361 2.4495
2.6458 2.8284 3.0000
我们可以把for循环里第三个代码用逻辑数组(矩阵)来实现:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14a=[1 2 3;4 5 6;7 8 9]; c=a>5; a(c)=sqrt(a(c)); i=0; j=0; for i=1:3 for j=1:3 if j>2 fprintf('%fn',a(i,j)); else fprintf('%ft',a(i,j)); end end; end;
那么两种方法那种效率更好呢?下面就来对比看看
for Loop:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15a=1:10000; b=reshape(a,100,100);%Devide a into 100*100 tstart=tic;%Time is strat for i=1:100 for j=1:100 if b(i,j)>3000 b(i,j)=sqrt(b(i,j)); end end end time_end=toc(tstart);%Time is over fprintf('the use of time is :%f',time_end); %%%%% result: the use of time is :0.003353>>;
logical:
复制代码
1
2
3
4
5
6
7
8
9
10a=1:10000; b=reshape(a,100,100); tstart=tic;%Time is strat c=b>3000; b(c)=sqrt(b(c)); time_end=toc(tstart);%Time is over fprintf('the use of time is :%f',time_end); %%%%%% result: the use of time is :0.002274>>
逻辑运算 要快些。
四、选择排序和冒泡排序
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43%1.选择排序 a=[1 2 -1 3 2 6 8 8 2 3 2 10]; n=length(a); for i=1:length(a)-1 for j=i+1:length(a) if(a(i)>a(j)) t=a(i); a(i)=a(j); a(j)=t; end end end disp('排序后:') a %2.冒泡排序 a=[1 2 -1 3 2 6 8 8 2 3 2 10]; n=length(a); for i=1:n-1 for j=1:n-i if a(j+1)<a(j) temp=a(j); a(j)=a(j+1); a(j+1)=temp; end end end disp('') a %3.matlab自带排序函数sort(); a=[1 2 -1 3 2 6 8 8 2 3 2 10]; b=sort(a,'descend');%升序排列 c=sort(a,'ascend');%降序排列 disp('') b c %result: 排序后: a = -1 1 2 2 2 2 3 3 6 8 8 10
对于sort()函数想了解更多,参考matlab文档。
最后
以上就是单薄眼睛最近收集整理的关于Matlab基础之矩阵循环一、for循环二、while循环三、逻辑数组四、选择排序和冒泡排序的全部内容,更多相关Matlab基础之矩阵循环一、for循环二、while循环三、逻辑数组四、选择排序和冒泡排序内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复