-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic_matlab_octave.m
130 lines (90 loc) · 1.82 KB
/
generic_matlab_octave.m
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
A = [1 2; 5 11; 17 65]
B= [1 9; 191 11; 16 65]
C = [A B]
C = [A; B]
size(C) %6 * 2
v = [1 2 3] % row
v = [1; 2; 3] % vector
abs(v)
v = v + ones(length(v), 1)
v + 1
A' %transpose of A
(A')'
a = [1 15 2 0.5]
val = max (a)
[val, ind] = max (a)
max(A)
a < 3 %element wise comparison returns true of false
find(a < 3) % returns indexes
A = magic(3) %row and col sums are equal
[r, c] = find(A >= 7) % finds row and cols acc to condition
sum(a)
prod(a)
floor(a)
ceil(a)
rand(3)
max(rand(3), rand(3)) % element wise max of 2 3*3 matrices
max(A, [], 1) % column wise max of A
max(A, [], 2) % row wise max of A
max(A) % column wise max of A
max(max(A)) % max among all elem of A
max(A(:)) % max among all elem of A
K = [0: 1: 8]
A = magic(9)
sum(A, 1) % column sum
sum(A, 2) % row sum
sum(sum(A .* eye(length(A)))) % diagonal sum
pinv(A) % pseudo inverse of A
%PLOTTING THE ALGORITHMS%
%Sin function
t = [0: 0.01: 0.98];
y1 = sin(2 * pi * 4 * t);
plot(t, y1);
y2 = cos(2 * pi * 4 * t);
plot(t, y2);
plot(t, y1); % draw a plot
hold on;
plot(t, y2, 'r');
xlabel('time');
ylabel('value');
legend('sin', 'cos');
title('myPlot');
cd print -dpmg 'myPlot.png' %to save the plot
figure(1);plot(t, y1); % draw on different figures
figure(2);plot(t, y2);
subplot(1, 2, 1); % divide figure 1*2 grid, access first element
plot(t, y1);
subplot(1, 2, 2);
plot(t, y2);
axis([0.5 1 -1 1]); % change x and y axis of plots
clf; % clear figure
A = magic(5);
imagesc(A);
imagesc(A), colorbar, colormap gray;
a = 1, b = 2, c = 3
% for loop
v = zeros(10, 1);
for i = 1:10
v(i) = 2 ^ i;
end;
indices = 1 : 10;
for i = indices;
disp(i);
end;
i = 1;
while i <= 5,
v(i) = 100;
i = i + 1;
end;
v;
i = 1;
while true,
v(i) = 999;
if i == 6,
break;
end;
end;
v;
squareThisNumber(5)
%Octave search path add new path
addpath(dir)