星期二, 11月 14, 2006

work-5

第一題Create a 3X3X2 matrix A which contains a matrix of magic(3)
and another rand(3,3)*10.

程式碼
a=cat(3,magic(3),rand(3,3)*10)
結果
a(:,:,1) =
8 1 6
3 5 7
4 9 2

a(:,:,2) =
8.4622 6.7214 6.8128
5.2515 8.3812 3.7948
2.0265 0.1964 8.3180


第二題 Assume that matrices A=[45 89 99; 12 34 55], B=[15 25 45; 65 50 30].
Find matrices that join A & B both in vertical and horizontal directions.
Also, find a two-page matrix with A & B stored in separate pages.
Use commands horizcat and verticat to check the results with those from above


程式碼
(1)

A=[45 89 99;12 34 55];
B=[15 25 45;65 50 30];

disp('用[A;B]組合成vertical')
v=[A;B] % 組合成vertical
disp('用cat(1,A,B)組成的vertica')
V=cat(1,A,B) % 利用cat funcation 組成的vertical

disp('證明')
v==V % 證明是否一樣

結果
(1)
用[A;B]組合成vertical
v =
45 89 99
12 34 55
15 25 45
65 50 30
用cat(1,A,B)組成的vertica
V =
45 89 99
12 34 55
15 25 45
65 50 30
證明
ans =
1 1 1
1 1 1
1 1 1
1 1 1
所以是用這二種方式結果是一樣的

程式碼
(2)

disp('用[A B]組合成horizontal')
h=[A B] % 組合成horizontal
disp('cat(2,A,B)')
H=cat(2,A,B) % 利用cat funcation 組成的horizontal

disp('證明')
H==h % 證明是否一樣
結果(2)
用[A B]組合成horizontal
h =
45 89 99 15 25 45
12 34 55 65 50 30

cat(2,A,B)
H =
45 89 99 15 25 45
12 34 55 65 50 30

證明
ans =
1 1 1 1 1 1
1 1 1 1 1 1
所以是用這二種方式結果是一樣的

程式碼
(3)
disp('p(:,:,1)=A')
p(:,:,1)=A; % 組合成二頁,第一頁
disp('p(:,:,2)=B')
p(:,:,2)=B; % 組合成二頁,第二頁
p
disp('cat(3,A,B)')
P=cat(3,A,B) % 利用cat funcation 組成頁

disp('證明')
p==P% 證明是否一樣

結果(3)
p(:,:,1)=A
p(:,:,2)=B

p(:,:,1) =
45 89 99
12 34 55

p(:,:,2) =
15 25 45
65 50 30

cat(3,A,B)
P(:,:,1) =
45 89 99
12 34 55

P(:,:,2) =
15 25 45
65 50 30

證明
ans(:,:,1) =
1 1 1
1 1 1
ans(:,:,2) =
1 1 1
1 1 1
所以是用這二種方式結果是一樣的

第三題Find a matrix M which contains A as the first and third pages
and B the second and fourth
pages
程式碼
close all
clear all
clc
A=[45 89 99;12 34 55];
B=[15 25 45;65 50 30];
M(:,:,1)=A;% 第一頁
M(:,:,3)=A;% 第三頁
M(:,:,2)=B;% 第二頁
M(:,:,4)=B;% 第四頁
M
cat(3,A,B,A,B)%用cat組成四頁
結果
M(:,:,1) =
45 89 99
12 34 55
M(:,:,2) =
15 25 45
65 50 30
M(:,:,3) =
45 89 99
12 34 55
M(:,:,4) =
15 25 45
65 50 30

ans(:,:,1) =
45 89 99
12 34 55
ans(:,:,2) =
15 25 45
65 50 30
ans(:,:,3) =
45 89 99
12 34 55
ans(:,:,4) =
15 25 45
65 50 30
由止可知上面二種方法都可以
>>


第四題Construct a 2X2 cell array
that contains 'Eric' [90 100]; 'Peter' [35 60]; 'Jan' [77 67]

程式碼
A(1,1)={'name'};%自己加的因為是2X2所以把空格給填滿
A(1,2)={'score'};%自己加的
A(2,1)={{'Eric';'Jan';'Peter' }};
A(2,2)={[90 100;35 60;77 67]};
celldisp(A)
cellplot(A)

結果
A{1,1} =
name
A{2,1}{1} =
Eric
A{2,1}{2} =
Jan
A{2,1}{3} =
Peter
A{1,2} =
score
A{2,2} =
90 100
35 60
77 67


第五題Construct a structural Array that contains the following data:
Name: 'Philip', 'Peter','Roberts', 'Roe'
Age: 35, 45, 55, 60
Salary: 50,000 40,000 80,000 120,000


程式碼
DATA=struct('Name',{'philip','Peter','Roberts','Roe'},'Age',{35,45,55,60},'Salary',{50000,40000,80000,120000})
for N=1:4 %show 出內容
DATA(N)
end
結果
DATA =
1x4 struct array with fields:
Name
Age
Salary
ans =
Name: 'philip'
Age: 35
Salary: 50000
ans =
Name: 'Peter'
Age: 45
Salary: 40000
ans =
Name: 'Roberts'
Age: 55
Salary: 80000
ans =
Name: 'Roe'
Age: 60
Salary: 120000

1 則留言:

ncbee 提到...

作業5-1 姓名:黃聖峰 學號:r95631002
第一題Create a 3X3X2 matrix A which contains a matrix of magic(3)
and another rand(3,3)*10.

程式碼
a=cat(3,magic(3),rand(3,3)*10)
結果
a(:,:,1) =
8 1 6
3 5 7
4 9 2

a(:,:,2) =
8.4622 6.7214 6.8128
5.2515 8.3812 3.7948
2.0265 0.1964 8.3180


第二題 Assume that matrices A=[45 89 99; 12 34 55], B=[15 25 45; 65 50 30].
Find matrices that join A & B both in vertical and horizontal directions.
Also, find a two-page matrix with A & B stored in separate pages.
Use commands horizcat and verticat to check the results with those from above

程式碼
(1)
A=[45 89 99;12 34 55];
B=[15 25 45;65 50 30];

disp('用[A;B]組合成vertical')
v=[A;B] % 組合成vertical
disp('用cat(1,A,B)組成的vertica')
V=cat(1,A,B) % 利用cat funcation 組成的vertical

disp('證明')
v==V % 證明是否一樣

結果
(1)
用[A;B]組合成vertical
v =
45 89 99
12 34 55
15 25 45
65 50 30
用cat(1,A,B)組成的vertica
V =
45 89 99
12 34 55
15 25 45
65 50 30
證明
ans =
1 1 1
1 1 1
1 1 1
1 1 1
所以是用這二種方式結果是一樣的

程式碼
(2)
disp('用[A B]組合成horizontal')
h=[A B] % 組合成horizontal
disp('cat(2,A,B)')
H=cat(2,A,B) % 利用cat funcation 組成的horizontal

disp('證明')
H==h % 證明是否一樣
結果(2)
用[A B]組合成horizontal
h =
45 89 99 15 25 45
12 34 55 65 50 30

cat(2,A,B)
H =
45 89 99 15 25 45
12 34 55 65 50 30

證明
ans =
1 1 1 1 1 1
1 1 1 1 1 1
所以是用這二種方式結果是一樣的

程式碼
(3)
disp('p(:,:,1)=A')
p(:,:,1)=A; % 組合成二頁,第一頁
disp('p(:,:,2)=B')
p(:,:,2)=B; % 組合成二頁,第二頁
p
disp('cat(3,A,B)')
P=cat(3,A,B) % 利用cat funcation 組成頁

disp('證明')
p==P% 證明是否一樣

結果(3)
p(:,:,1)=A
p(:,:,2)=B

p(:,:,1) =
45 89 99
12 34 55

p(:,:,2) =
15 25 45
65 50 30

cat(3,A,B)
P(:,:,1) =
45 89 99
12 34 55

P(:,:,2) =
15 25 45
65 50 30

證明
ans(:,:,1) =
1 1 1
1 1 1
ans(:,:,2) =
1 1 1
1 1 1
所以是用這二種方式結果是一樣的

第三題Find a matrix M which contains A as the first and third pages
and B the second and fourth pages
程式碼
close all
clear all
clc
A=[45 89 99;12 34 55];
B=[15 25 45;65 50 30];
M(:,:,1)=A;% 第一頁
M(:,:,3)=A;% 第三頁
M(:,:,2)=B;% 第二頁
M(:,:,4)=B;% 第四頁
M
cat(3,A,B,A,B)%用cat組成四頁
結果
M(:,:,1) =
45 89 99
12 34 55
M(:,:,2) =
15 25 45
65 50 30
M(:,:,3) =
45 89 99
12 34 55
M(:,:,4) =
15 25 45
65 50 30

ans(:,:,1) =
45 89 99
12 34 55
ans(:,:,2) =
15 25 45
65 50 30
ans(:,:,3) =
45 89 99
12 34 55
ans(:,:,4) =
15 25 45
65 50 30
由止可知上面二種方法都可以
>>


第四題Construct a 2X2 cell array
that contains 'Eric' [90 100]; 'Peter' [35 60]; 'Jan' [77 67]
程式碼
A(1,1)={'name'};%自己加的因為是2X2所以把空格給填滿
A(1,2)={'score'};%自己加的
A(2,1)={{'Eric';'Jan';'Peter' }};
A(2,2)={[90 100;35 60;77 67]};
celldisp(A)
cellplot(A)

結果
A{1,1} =
name
A{2,1}{1} =
Eric
A{2,1}{2} =
Jan
A{2,1}{3} =
Peter
A{1,2} =
score
A{2,2} =
90 100
35 60
77 67
圖片

第五題Construct a structural Array that contains the following data:
Name: 'Philip', 'Peter','Roberts', 'Roe'
Age: 35, 45, 55, 60
Salary: 50,000 40,000 80,000 120,000

程式碼
DATA=struct('Name',{'philip','Peter','Roberts','Roe'},'Age',{35,45,55,60},'Salary',{50000,40000,80000,120000})
for N=1:4 %show 出內容
DATA(N)
end
結果
DATA =
1x4 struct array with fields:
Name
Age
Salary
ans =
Name: 'philip'
Age: 35
Salary: 50000
ans =
Name: 'Peter'
Age: 45
Salary: 40000
ans =
Name: 'Roberts'
Age: 55
Salary: 80000
ans =
Name: 'Roe'
Age: 60
Salary: 120000
看不到按我

花蓮的海豚

花蓮的海豚