台大郭彦甫matlab学习笔记01

%% Topic 0306
% introduction
% matlab as calculator
% array operation

% 作为计算器使用,主要在命令行
% 操作号:+ - * / ^ 
% The result is computed, and displayed as ans
% Precedence rules:
    %Left-to-right within a precedence group    
    %precedence groups are Highest first
    % parenthesis()
    % power ^
    % multiplication and division * /
    % addition and subtraction + - 
% exercise 01

exer1=cos(((1+2+3+4)^3/5)^0.5);
exer2=sin(pi^0.5)+log(tan(1));
exer3=2^(3.5*1.7);
exer4=exp(sin(10));

% your best friend --- on-line help
% www.mathworks.com/help/matlab
% see also ……

% elementary math functions
% www.mathworks.com/help/matlab/functionlist.html
    % Arithmetic
    % Trigonometry
    % Exponents and Logarithms
    % Complex numbers
    % Cartesian Coordinate System Conversion
% Embedding Functions:functions may be embedded into other function
% Many lines of code can be condensed into one single command

%Variables
    % Variables do NOT need to be declared before assignment
    % A single "equal" sign(=) is the assignment operator: LHS must be a
    % vriable, NOT a number, RHS may variable and number.
    % Upper case and Lower case make difference
    % variable name cannot begin with a number

% Variable data Type(multidimensional array)
    % logical
    % char
    % numberic
        % int 8 uint8 single double
        % int16 uint16
        % int32, uint32
        % int64, uint64
    % cell
    % struct

    % scalar
    % function handle(@)

% Special Varibles and Varibales constants
    % Varibles:
        % ans: 运算后没有指定变量名的结果的临时存储名称
        % i,j complex number
        % inf: 无穷
        % eps: 2.2204e-016
        % NaN: not a number
        %pi: 圆周率pi
    % Keywords: matlab中的关键字,变量名的暗坑
        % iskeyword
    % calling priority, from high to low
        % variable
        % built-in function
        % subfuntion
        % private function
            % mex-file
            % p-file
            % m-file

% numeric display 'Format'
    % short fixed-decimal format with 4 digits after the decimal point 3.1416
    % long doule:fixed-decimal format with 15 digits after the decimal point
    % long single:fixed-decimal format with 7 digits after the decimal point
    % shortE short scientific notation with 4 digits after the decimal point 
    % longE double: long scientific notation with 15 digits after the decimal point 
    % longE single: long scientific notation with 7 digits after the decimal point 
    % bank currency format with 2 digits after the decimal point
    % hex hexadecimal representation of a binary double-precision number
    % rat ratio of small integers

% exercis use different format
exer5=3/13+4/14+5/15;

% Command Line Terminal
    % Observe the difference between
    % ; at the end of a command suppresses output to the terminal
    % \uparrow display previous commands
    
% Some useful functions
    % clc clear  command window display
    % clear remove all varibales in the workspace
    % who variable in the workspace
    % whos variable information of the workspace

% Array(Vector and Matrix)
    % Row vector: elements sparator is , and space
    % Column vector: elements sparator is ;
    % row*column not equal colum*row
    % key in matrix use of [], the elements in a row sparator is , and space, the row element in different row sparator is ;

    %%
% Array indexing
    % single, the number of after linearized index
    % multiple, 
A=[1 21 6;5 17 9;31 2 7];
A8=A(8);
A135=A([1 3 5]);
A13_15=A([1 3],[2 3]);  %等效为两个索引,第一个表示行,第二个表示列
A32=A(3,2);
A([1 3;2 3]);%等效为一个索引,只不过构成两行两列的矩阵

% use mod function make a function: linearize index of matrix
function [rows, columns]=lineIndex(A,n)
    rows=mod(n,size(A,1));
    columns=ceil(n/size(A,1));
end

[r,c]=lineIndex(A,8)
    
% replac entries, matlab array vector
A(A>10)=10  %将A中所有大于10的元素全部替换为10

%% colon operator,number and chars
    % want to create a long array A=[1:10] 10 elements
    % creates vectors or arrays, and specifiy for iterations
clc;
clear all;
close all;

A=[1 21 6;5 17 9;31 2 7];
B1=1:5
B2=1:2:5
B3=[1:5;2:3:15;-2:0.5:0]
str='a':2:'z'

%clear data 
A(A>10)=0;
%index using colon operator,get data
A(2:3,:); 
%modify data,delete row and column
A1=A;
A2=A;
A1(:,1)=[]
A2(1,:)=[]
%concatenation array
C=[2 1;3 2;-2 2];
D=[1 1;3 4;2 2];
%column concat
E=[C D]  
%row concat
F=[C;D]

%complesive concat
G1=[1 2;3 4];
G2=[9 9;9 9];
G3=[5:8];
G4=[-2:1];
G=[G1 G2;G3;G4]

%% array manipulation
    %operators on array:+ - * / ^ \ .* ./ .^ '
clc;
clear all;
close all;

% matrix
A=[1:3;4:6;9:-1:7];
A(A==6)=4;
B=[3 3 3;2 4 9;1 3 1];
% scalar 
a=2;
x1=A+a;
y1=A-B;
x2=A*a;
y2=A/B;
Y2=A\B;
x3=A^a;
y3=A*B;
x4=A/a;
y4=A./B;
x5=A./a;
y5=A.*B;
z=A';

%% array manipulation
    % array scalar add subtraction A+b, A-b
    % array addition and subtraction A+B, A-B
    % array multiplication A*B \neq B*A must the column of A equal the row
    % of B
    % array dot multiplication A.*B=B.*A
    % array left dot division A.\B
    % array right dot division A./B
    % array exponentiation A.^B
clc;
clear all;
close all;

C=[2 5];
D=[4 3];
b=3;
C*D'%=23
C'*D % [8 6;20 15]
C.*D %[8 15] 对应元素相乘
C./D %[0.5 1.667] 对应元素相除,C为被除数 右除
C.\D %[2 .6] 对应元素相除,D为被除数 左除
D.^C %[16 243] 对应元素幂运算

%% some special matrix
% eye zeros ones diag:对角线元素
clc;
clear all;
close all;

n1=3;
n2=4;

eye(n1)
zeros(n1,n2)
ones(n1,n2)
A=[1 2;3 4];
diag(A)

%% matrix related functions
%max min sum mean size length find sort sortrows
clc;
clear all;
close all;

A=[1:3;4:6;7:9];
A(mod(A,4)==0)=0;

%A =
%     1     2     3
%     0     5     6
%     7     0     9

max(A)  % A column max
max(max(A)) % the max element of A
min(A)  % A column min
sum(A)  % A column sum
mean(A) % A column mean
find(A) % A nonezero element's index
size(A) % the row and column of A
sort(A) % A column sort
sortrows(A)% A row sort

 

posted @ 2025-03-07 13:28  叕叒双又  阅读(26)  评论(0)    收藏  举报