Basic Command
~= %not equal
&& %and
|| %or
a=3 %semicolon supressing output
b=‘hi’
disp(a) %display value a
disp(sprint(‘2 decimals: %0.2f’,a)) %display 2 digits
A = [1 2; 3 4; 5 6] % generate a matrix
v = 1:0.1:2
v = 1:6
ones(2,3)
C = 2ones(2,3) % generate 23 1 matrix
w = rand(1,3) %randomly generate 1*3 matrix
I = eye(6) %6 *6 I matrix
size(A) % size of matrix
length(A) % length of a vector
load xxxx.file % load the data
load(‘xxxxx’)
who whos %shows all the variables
clear % delete a variable
v = a(1:10) % show first 10 of a
save file.txt v % save as v
A(3,2) % the 3rd row and 2nd column of A
A(2,:) % second row of A
A([1,3]😅
A(:,2) = [10;11;12] % attribute value
A = [A, [100,101,102]] % append the matrix to the right
A(😃 % put all elements of A into a single vector
C = [A B] or C = [A,B] % connect A and B side by side
C = [A;B] % connect A and B bottom and up
Caculation
A .^ 2 %all the elements square inA
1 ./ A % reciprocal of A
log(v)
V + ones(length(v),1) %add all in V by one
A’ % transpose
[val,ind] = max(a) % return max and position
A < 3 $ boolen judgement
find(a ❤️) % return position
pro, sum, ceil, floor
max(A,[],2] % 1 COLUMN MAXIUM 2 ROW MAXIUM
pinv(A) % find invert of A
Plot
t = [0:0.1:0.98]
y2 = sin(t)
plot(t,y2,‘r’) % color
hold on; %hold the layer
legend, xlabel, ylabel
subplot
imagesc(A) %show the matrix
imagesc(A),colorbar,colormap gray;
Control Statement
for i = 1:10,
v(i) = 2 ^ i;
end;
while i <= 5,
v(i) = 100;
i = i + 1;
end;
while true,
v(i) = 999;
i = i + 1;
if i == 6,
break;
end;
end;
if v(1) ==1,
disp(‘The value is one’);
elseif v(1) ==2,
disp(‘The value is two’);
end;
Function File Name with .m in the file name
Write the function in the file