62页课后习题2(1)
A=rand(3)*10;
>> [a,b] = size(A);
for i = a*b
while(1)
if (A(i)>=1)
break
else
A(i) = rand(1)*10;
end
end
end
B = sort(A,1,'descend')
B =
9.5929 5.4722 8.4072
8.9090 1.4929 2.5751
6.9908 1.3862 2.5428
save和load
load('matlab.mat')
2.A = rand(3),B = ones(3),采用cat函数对A、B矩阵进行列拼接得到矩阵C,计算矩阵D,其中D为C矩阵的第2行,第4列到第6列的元素。
>> A=rand(3)
A =
0.0759 0.7792 0.5688
0.0540 0.9340 0.4694
0.5308 0.1299 0.0119
>> B=ones(3)
B =
1 1 1
1 1 1
1 1 1
>> C=cat(2,A,B)
C =
0.0759 0.7792 0.5688 1.0000 1.0000 1.0000
0.0540 0.9340 0.4694 1.0000 1.0000 1.0000
0.5308 0.1299 0.0119 1.0000 1.0000 1.0000
>> D=C(2,[4,5,6])
D =
1 1 1
3、62页课后题4
>> A = rand(3)
A =
0.0965 0.9561 0.2348
0.1320 0.5752 0.3532
0.9421 0.0598 0.8212
>> find(A>0.5)
ans =
3
4
5
9
>> [a,b] = find(A>0.5)
a =
3
1
2
3
b =
1
2
2
3
4、采用rand函数生成两个3,3,3的多维数组A,B,计算A+B
>> A=rand(3,3,3)
A(:,:,1) =
0.3371 0.3112 0.6020
0.1622 0.5285 0.2630
0.7943 0.1656 0.6541
A(:,:,2) =
0.6892 0.0838 0.1524
0.7482 0.2290 0.8258
0.4505 0.9133 0.5383
A(:,:,3) =
0.9961 0.1067 0.7749
0.0782 0.9619 0.8173
0.4427 0.0046 0.8687
>> B=rand(3,3,3)
B(:,:,1) =
0.0844 0.8001 0.1818
0.3998 0.4314 0.2638
0.2599 0.9106 0.1455
B(:,:,2) =
0.1361 0.5499 0.6221
0.8693 0.1450 0.3510
0.5797 0.8530 0.5132
B(:,:,3) =
0.4018 0.1233 0.4173
0.0760 0.1839 0.0497
0.2399 0.2400 0.9027
>> A+B
ans(:,:,1) =
0.4216 1.1113 0.7838
0.5620 0.9599 0.5268
1.0542 1.0763 0.7996
ans(:,:,2) =
0.8253 0.6337 0.7744
1.6174 0.3739 1.1768
1.0302 1.7664 1.0516
ans(:,:,3) =
1.3979 0.2300 1.1922
0.1541 1.1458 0.8670
0.6826 0.2446 1.7714
5、.A = magic(5),利用find函数查找矩阵A中大于7小于13的元素的下标索引,找出矩阵A中大于8小于14的元素
>> A = magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
find((A>7)&(A<13))
ans =
4
5
9
16
25
A(find((A>8)&(A<14)))
ans =
10
11
12
13
9
6、,,计算A.^C, B.^A
>> A=[1 2 3;2 1 3;3 1 2]
A =
1 2 3
2 1 3
3 1 2
>> C=[4 3 1;1 5 7;2 1 3]
C =
4 3 1
1 5 7
2 1 3
>> B=2
B =
2
>> A.^C
ans =
1 8 3
2 1 2187
9 1 8
>> B.^A
ans =
2 4 8
4 2 8
8 2 4
- 采用complex建立一个复数数组和直接创立一个复数数组,并计算它们的幅度
complex(2)
ans =
2.0000 + 0.0000i
>> A=2+3i
A =
2.0000 + 3.0000i
>> abs(A)
ans =
3.6056
- 将A中大于0.8的数置0
>> A=[0.8147 0.6324 0.9575 0.9572;0.9508 0.0975 0.9649 0.4854;0.1270 0.2785 0.1576 0.8003;0.9134 0.5469 0.9706 0.1419]
A =
0.8147 0.6324 0.9575 0.9572
0.9508 0.0975 0.9649 0.4854
0.1270 0.2785 0.1576 0.8003
0.9134 0.5469 0.9706 0.1419
>> A(A>0.8)=0
A =
0 0.6324 0 0
0 0.0975 0 0.4854
0.1270 0.2785 0.1576 0
0 0.5469 0 0.1419
9、A =17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
,利用find函数查找找出矩阵A中大于8小于14的索引及元素
A=[17 24 1 8 15;23 5 7 14 16;4 6 13 20 22;10 12 19 21 3;11 18 15 2 9]
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 15 2 9
>> find((A>8)&(A<14))
ans=
4
5
9
13
>>A( find((A>8)&(A<14)))
ans=
10
11
12
13
41页第八题
a=39;
b=58;
c=3;
d=7;
disp('The surface area of the colume is:'),disp(a>b);
disp('The surface area of the colume is:'),disp(a<c);
disp('The surface area of the colume is:'),disp(a>b&&b>c);
disp('The surface area of the colume is:'),disp(a==d);
disp('The surface area of the colume is:'),disp(a|b>c);
disp('The surface area of the colume is:'),disp(~~d);
The surface area of the colume is:
0
The surface area of the colume is:
0
The surface area of the colume is:
0
>> Untitled
The surface area of the colume is:
0
The surface area of the colume is:
0
The surface area of the colume is:
0
The surface area of the colume is:
0
The surface area of the colume is:
1
The surface area of the colume is:
1
10、a=22,b=18,计算a和b的按位与,按位抑或
>> a=22,b=18
a =
22
b =
18
>> C=bitand(a,b)
C =
18
>> D=bitxor(a,b)
D =
4
62页1
>> A = rand(3)
A =
0.2399 0.2400 0.9027
0.1233 0.4173 0.9448
0.1839 0.0497 0.4909
>> A(1,1) = A(1,1)+1;
>> A(2,2) = A(2,2)+1;
>> A(3,3) = A(3,3)+1;
>> A
A =
1.2399 0.2400 0.9027
0.1233 1.4173 0.9448
0.1839 0.0497 1.4909
62页2
>> A = rand(3)*10;
[a,b] = size(A);
for i = a*b
while(1)
if (A(i)>=1)
break
else
A(i) = rand(1)*10;
end
end
end
B = sort(A,1,'descend')
C = sort(A,2,'ascend')
B =
9.0005 7.8025 4.0391
4.8925 3.6925 3.8974
3.3772 1.1120 2.4169
C =
3.6925 3.8974 4.8925
1.1120 2.4169 3.3772
4.0391 7.8025 9.0005
62页3
>> a = 3;X = [2 1;3 2];Y = [4 5;6 7];
>> a.^X
ans =
9 3
27 9
>> X.^a
ans =
8 1
27 8
>> X.^Y
ans =
16 1
729 128
62页4
>> A = rand(3)
A =
0.0965 0.9561 0.2348
0.1320 0.5752 0.3532
0.9421 0.0598 0.8212
>> find(A>0.5)
ans =
3
4
5
9
>> [a,b] = find(A>0.5)
a =
3
1
2
3
b =
1
2
2
3
62页5
>> a = 3; B = [4 3; 7 5];C = [8 9;7 6];D = [4 7;12 8];
>> ~(a<B)
ans =
0 1
0 0
>> a>C&B<D
ans =
0 0
0 0
>> C<=D
ans =
0 0
1 1