>> % Basic algebra
>> 5+6
ans =
11
>> 3-1
ans =
2
>> 5*8
ans =
40
>> 1/2
ans =
0.500000000000000
>> 2^6
ans =
64
>> 1==3
ans =
0
>> 33==33
ans =
1
>> 1~=5
ans =
1
>> xor(1,5)
xor(4,4)
xor(1,0)
ans =
0
ans =
0
ans =
1
>> a=3
a=3;
b='hi'
a =
3
b =
hi
>> a=pi;
a
a =
3.141592653589793
>> disp(a)
disp(sprintf('1 decimals: %.1f',a))
disp(sprintf('10 decimals: %.10f',a))
format long
a
3.141592653589793
1 decimals: 3.1
10 decimals: 3.1415926536
a =
3.141592653589793
>> %Vectors and matrices
A=[1 2;3 4;5 6]
V=[1 2 3]
V=[1; 2; 3]
A =
1 2
3 4
5 6
V =
1 2 3
V =
1
2
3
>> v=1:.1:2
v=1:6
ones(2,3)
c=2*ones(2,3)
c=[2 2 2;2 2 2]
v =
1 列から 2 列
1.000000000000000 1.100000000000000
3 列から 4 列
1.200000000000000 1.300000000000000
5 列から 6 列
1.400000000000000 1.500000000000000
7 列から 8 列
1.600000000000000 1.700000000000000
9 列から 10 列
1.800000000000000 1.900000000000000
11 列
2.000000000000000
v =
1 2 3 4 5 6
ans =
1 1 1
1 1 1
c =
2 2 2
2 2 2
c =
2 2 2
2 2 2
>> v=ones(1,3)
v=zeros(1,3)
v=rand(1,3)
rand(3,3)
randn(1,3) %Gaussian distribution
v =
1 1 1
v =
0 0 0
v =
1 列から 2 列
0.461780008104866 0.233904981088473
3 列
0.648465368199192
ans =
1 列から 2 列
0.186249089220997 0.996978106422137
0.213256922922233 0.816995477836564
0.137547804971539 0.655195949786273
3 列
0.589312329593177
0.593125549822003
0.430980180307123
ans =
1 列から 2 列
0.086235785216599 -0.474083443064586
3 列
-1.517982173979169
>> >> w=-6+sqrt(10)*(randn(1,10000));
>> hist(w)
>> hist(w,50)
%a line-vector with 10000 elements. These ele-
%ments correspond to a Gaussian distribution of average = ??6 and standard deviation =
%p 10 or, which isequivalent, a variance 2 equal to 10
>>
>> A=[1 2; 3 4;5 6]
size(A)
sz=size(A)
size(sz)
size(A,1)
size(A,2)
v=[1 2 3 4]
length(v)
length(A)
length([1;2;3;4;5])
A =
1 2
3 4
5 6
ans =
3 2
sz =
3 2
ans =
1 2
ans =
3
ans =
2
v =
1 2 3 4
ans =
4
ans =
3
ans =
5
>> >> pwd
ls
A([1 3],:)
A(:,2)=[10,11,12]
A=[A,[100;101;102]]
ans =
C:\MATLAB\Matlab Practice
.
..
Basic Operation.m
ans =
1 2
5 6
A =
1 10
3 11
5 12
A =
1 10 100
3 11 101
5 12 102
>>
>> pwd
ls
A([1 3],:)
A(:,2)=[10,11,12]
A=[A,[100;101;102]]
ans =
C:\MATLAB\Matlab Practice
.
..
Basic Operation.m
ans =
1 2
5 6
A =
1 10
3 11
5 12
A =
1 10 100
3 11 101
5 12 102
>> >> size(A)
A(:)
A=[1 2; 3 4; 5 6];
B=[11 12;13 14;15 16];
C=[A B]
C=[A ;B]
size(C)
[A,B]
ans =
3 3
ans =
1
3
5
10
11
12
100
101
102
C =
1 2 11 12
3 4 13 14
5 6 15 16
C =
1 2
3 4
5 6
11 12
13 14
15 16
ans =
6 2
ans =
1 2 11 12
3 4 13 14
5 6 15 16
>>>> %Computing on Data
A.*B
A.^2
V=[1;2;3]
1./V
log(V)
exp(V)
abs(V)
-V
ans =
11 24
39 56
75 96
ans =
1 4
9 16
25 36
V =
1
2
3
ans =
1.000000000000000
0.500000000000000
0.333333333333333
ans =
0
0.693147180559945
1.098612288668110
ans =
2.718281828459046
7.389056098930650
20.085536923187668
ans =
1
2
3
ans =
-1
-2
-3
>> >> V+ones(length(V),1)
length(V)
ones(3,1)
V+1
A
A'
(A')'
a=[1 15 2 .5]
val=max(a)
[val,ind]=max(a)
max(A)
ans =
2
3
4
ans =
3
ans =
1
1
1
ans =
2
3
4
A =
1 2
3 4
5 6
ans =
1 3 5
2 4 6
ans =
1 2
3 4
5 6
a =
1 列から 2 列
1.000000000000000 15.000000000000000
3 列から 4 列
2.000000000000000 0.500000000000000
val =
15
val =
15
ind =
2
ans =
5 6
>> >> a
sum(a)
prod(a)
floor(a)
a =
1 列から 2 列
1.000000000000000 15.000000000000000
3 列から 4 列
2.000000000000000 0.500000000000000
ans =
18.500000000000000
ans =
15
ans =
1 15 2 0
>> >> ceil(a)
rand(3)
max(rand(3),rand(3))
A
ans =
1 15 2 1
ans =
1 列から 2 列
0.156783223153802 0.718082179863056
0.349706331243308 0.563396748427973
0.413712205235548 0.350080982114063
3 列
0.291090844074460
0.484555111305661
0.861027023541243
ans =
1 列から 2 列
0.890222489846095 0.906471776077859
0.956839428332754 0.373982540376440
0.962510521920430 0.730179099383832
3 列
0.957216928257528
0.595322395624069
0.355443735838794
A =
1 2
3 4
5 6
>> >> max(A,[],1)
max(A,[],2)
max(A)
max(max(A))
max(A(:))
ans =
5 6
ans =
2
4
6
ans =
5 6
ans =
6
ans =
6
>> >> A=magic(9)
sum(A,1)
sum(A,2)
A.*eye(9)
A =
47 58 69 80 1 12 23 34 45
57 68 79 9 11 22 33 44 46
67 78 8 10 21 32 43 54 56
77 7 18 20 31 42 53 55 66
6 17 19 30 41 52 63 65 76
16 27 29 40 51 62 64 75 5
26 28 39 50 61 72 74 4 15
36 38 49 60 71 73 3 14 25
37 48 59 70 81 2 13 24 35
ans =
369 369 369 369 369 369 369 369 369
ans =
369
369
369
369
369
369
369
369
369
ans =
47 0 0 0 0 0 0 0 0
0 68 0 0 0 0 0 0 0
0 0 8 0 0 0 0 0 0
0 0 0 20 0 0 0 0 0
0 0 0 0 41 0 0 0 0
0 0 0 0 0 62 0 0 0
0 0 0 0 0 0 74 0 0
0 0 0 0 0 0 0 14 0
0 0 0 0 0 0 0 0 35
>> >> sum(sum(A.*eye(9)))
sum(sum(A.*flipud(eye(9))))
eye(9)
flipud(eye(9))
A=magic(3)
pinv(A)
inv(A)
pinv(A)*A
A*pinv(A)
ans =
369
ans =
369
ans =
1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1
ans =
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
A =
8 1 6
3 5 7
4 9 2
ans =
1 列から 2 列
0.147222222222223 -0.144444444444445
-0.061111111111112 0.022222222222224
-0.019444444444444 0.188888888888888
3 列
0.063888888888889
0.105555555555555
-0.102777777777777
ans =
1 列から 2 列
0.147222222222222 -0.144444444444444
-0.061111111111111 0.022222222222222
-0.019444444444444 0.188888888888889
3 列
0.063888888888889
0.105555555555556
-0.102777777777778
ans =
1 列から 2 列
1.000000000000003 -0.000000000000000
-0.000000000000006 1.000000000000000
0.000000000000003 0.000000000000001
3 列
-0.000000000000003
0.000000000000006
0.999999999999997
ans =
1 列から 2 列
1.000000000000006 -0.000000000000012
0.000000000000000 1.000000000000000
-0.000000000000006 0.000000000000012
3 列
0.000000000000006
0
0.999999999999994
>> >> hold on;
>> plot(t,y2);
figure(1);plot(t,y1);
figure(2);plot(t,y2);
clf;
A=magic(5)
imagesc(A)
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
>> imagesc(magic(15)),colorbar,colormap gray;
a=1,b=2,c=3
v=zeros(10,1);
a =
1
b =
2
c =
3
>> for i=1:10,
v(i)=2^i;
end
v
indices=1:10;
indices
v =
2
4
8
16
32
64
128
256
512
1024
indices =
1 列から 9 列
1 2 3 4 5 6 7 8 9
10 列
10
>> >> for i=1:10,
v(i)=2^i;
end
v
indices=1:10;
indices
v =
2
4
8
16
32
64
128
256
512
1024
indices =
1 列から 9 列
1 2 3 4 5 6 7 8 9
10 列
10
>> >> for i=1:10,
v(i)=2^i;
end
v
indices=1:10;
indices
v =
2
4
8
16
32
64
128
256
512
1024
indices =
1 列から 9 列
1 2 3 4 5 6 7 8 9
10 列
10
>> >> for i=indices,
disp(i);
end;
i=1;
while i<5 font="">5>
v(i)=100;
i=i+1;
end;
v
1
2
3
4
5
6
7
8
9
10
v =
100
100
100
100
32
64
128
256
512
1024
>> >> i=1;
while true,
v(i)=999;
i=i+1;
if i==6,
break;
end;
end;
v
v =
999
999
999
999
999
64
128
256
512
1024
>> >> v(1)
v(1)=2;
if v(1)==1,
disp('the value is one');
elseif v(1)==2,
disp('the value is two');
else
disp('the value is not one or two');
end;
ans =
999
the value is two
>>