Matlab data and its operations (quick review version)

1;%Auxiliary command
path(path,'xx')%Set the path
%;The semicolon does not display the result of the previous statement,...the three dots are the continuation character, % is the comment character
help %completely matches, and the m file displays
lookfor % range matching -all queries all, Tab can fuzzy query
doc xx% Use xml to view the syntax of a certain command,

2.1;%Data type
int();%Type conversion
complex();
real();%Find the real part
image()%Find the imaginary part
conj();%Find the conjugate complex number
class();%Data type
double;singel( )
format xx %Format a certain data type, only affects the output format, the default is short

2.2;% Matrix representation
A=[1,2,3,4,5,6];% Directly establish matrix
B=[6,5,4,3,2,1,];
C2=[A,B;B ,A];% Use matrices to construct larger matrices. This can be used to combine the real part matrix and the imaginary part matrix to form a complex matrix
D=A+B*1i;
D;

%The colon expression produces the row vector
t=0:1:5;
t1=linspace(1,5,5);%is equivalent to a:(ba)/(n-1):b

%Element reference, subscript reference and serial number reference, the latter adopts column sorting
A=[1,2,3;4,5,6];
A(4,5)=10; %If it exceeds the rows and columns of the original matrix, it will Automatic expansion, fill
A(3)=5 with 0 if the value has not been changed; %A(i,j)=A((j-1)*m+i)
D=sunb2ind(size(A),[1, 2;2,2],[1,1;3,2]);% Convert the specified row and column subscripts into serial numbers
[I,J]=ind2sub(size(A),[1,3,5]);%size (A)=[3,3]
length(A);%Find the longest dimension
ndims(A);%Find the dimension of the matrix
numel(A);%Find the number of elements of the matrix

%Colon expression obtains the submatrix
%A(i,j);A(i,:);A(:,j);A(i:i+m;j:j+m);A(i:i+ m,:);A(:,j:j+m),A(:),A(:,:) %
A(end,:);A([1,4],3:end)

%Use an empty matrix to delete matrix elements, or you can assign a certain interval to reduce the deletion
X=[];
A=[1:3;4:6;7:9];
A(:,[2,4])=[ ];% cannot =X, the matrix size does not match
A;

%Change the matrix shape
A=1:1:12;
Z=reshape(A,2,6);%Do not change the logical structure and storage order, that is, still change
Z(:) according to column sorting;%Become a column vector , equivalent to reshape(A,12,1), row vector transpose Z'
% row vector transformation [G(1,:)';G(2,:)';G(3,:)']

2.3;%Variables and their operations
%_Underscores cannot be placed at the beginning, but can only be placed at the end. They are case-sensitive.
%ans assigns variables by default, i and j represent imaginary units, pi pi, NaN represents not a number, eps floating point relative precision, inf infinity Inf
bianliang=3;
3;%ans=3
y1=exp(pi/2);%e to the power of pi/2 z
=cos(47*pi/180);%cos47°
%predefined variable ans, eps,pi,i,j,inf,Inf,NaN,nan....
who % Check which variables there are
whos % Same as above and give the size, number of bytes occupied, data type and other information
clear % Delete the variables in the workspace All variables
% can create new variables in the workspace and store large matrices
%save path file name [variable name table] -append -ascii; The load command means loading variables into the workspace


2.4;%Internal function
abs();%Absolute value, modulus of a complex number, ASCII code value of the first letter of a string
round();
x(:);%==x,x(95:99) continuously refers to an interval element ,x([98,95,93])
%sqrt,log,log10,log2,exp,pow2,rem,mod,fix.floor,ceil,sign,gcd,lcm,factorial,isprime,primes,perms,sin, cos, tan
%rem finds the remainder quotient fix and rounds it towards 0, mod finds the remainder rounding towards the floor direction, rem(a,0)=NaN,mod(a,0)=a

%Transcendental function used for matrices, add m after the above function name, and the parameters need to be square matrices
A=[1,2;3,4];
B=sqrtm(A);
res10=B*B;
%Real symmetry Positive definite matrices and complex Hermitian positive definite matrices can get square roots. If A contains negative eigenvalues, sqrtm will get the complex matrix
A=[4,9;16,25];
eig(A);
B1=sqrtm(A) ;
%logm(),expm(), the two are inverse to each other
% Ordinary matrix function
funm(A,@exp);% Equivalent to expm(A), but funm cannot be used to find the square root of a matrix, only sqrtm

2.5;%MATLAB operation
A=[1,2;3,4];
B=[5,6;7,8];
R1=A+B;%addition and subtraction
R2=A+2;
R3=AB;
R4 =A-2;
R5=A*B;% multiplication
R6=A*2;
R7=A\B;% division, equivalent to inv(A)*B
R8=A/B;
R9=A/2;
R10 =2\A;
R11=A^2;% power, square root
R12=A^0.1;
R13=A.*B;% point operation, refers to the operation of the corresponding elements of the matrix, regardless of the matrix rules, the matrix type is required Identical
%.* ;./ ;.\ ;.^ There are 4 types in total, but the values ​​of A./B and B.\A are equal, the exponent can be a scalar, and the base can also be a scalar A=[1,2
, 3];
B=[4,5,6];
C=A.^2;
C1=2.^[AB];
y=sin(A).*cos(A);

% Relational operations: < <= > >= == ~=,lt,le,gt,ge,eq,ne
eq(2,3)% The scalar is directly compared to the size. The corresponding element ratio of the same type matrix is ​​01 matrix, and the scalar sum The matrix ratio is the same as
%all (all 1 means 1), any (all 1 means 1), find, exist, isempty, isfloat...
%all(all()) compares two matrices to see if they are equal. For a result, the vector only needs one all
A=[5,6,2];
K=find(A>4);
res=A(K);

% Logical operations: & | ~ ; and() or() not() xor(), the results refer to the explanation given in relational operations
A=[4,65,-54,0,6];
B=[0,5 ,3,2,-6];
res1=xor(A>10,B<10);% value is 0 for the same, 1 for different, different from or

2.6;%String
str1='I like you ';
str2='I''m your father';
res3=str1(1:3);
str3=[str1;str2];%String matrix, it is necessary to ensure that each row Strings are equal, often filled with spaces
res4=str3(2,3);
reverse=str1(end:-1:1);%reverse
k=find(str1>'a' & str1<'z');
str1( k)=str1(k)-('a'-'A');% equivalent to +('A'-'a'),a=A+32 res5=length
(k);

%String operations and numerical conversion of strings
t=pi;
m='[t,sin(t),cos(t)]';
y3=eval(m);
str4='MATLAB';
temp1=abs(str4 );
str5=char(temp1+32);

%String concatenation and string comparison and search and replacement
str6=['I','am','man'];%String vector method
str7=strcat('I','am','man');% strcat function method
res6='www0'>='w123';%If the string lengths are the same, compare the ASCII code of each character to get the numerical vector
strcmp(s1,s2);%Compare whether the strings S1S2 are equal, wait 1, no etc. 0
strncmp(s1,s2,n);% Compare the first n characters for equality
strcmpi(s1,s2);%Ignore the case
strncmpi(s1,s2,n);
strfind(s1,s2); %Return the starting position of the short string in the long string findstr
strrep(s1,s2,s3)%Replace all substrings S2 in S1 with S3

2.7;% Structural data and unit data
a(1).x1=10;a(1).x2='zuo';a(1).x3=[1,2,3];%Member reference assignment to obtain structural data
a(2).x1=12;a(2).x2='liu';a(2).x3=[4,5,6]; a(1
).x1.x11=90;a(1) .x1.x12=70;a(1).x1.x13=165;
a(1).x4='add';%Add and assign
a=rmfield(a,'x4');%Delete

b={10,'liu',[11,21;34,78];12,'wang',[34,191;27,578];14,'cai',[13,890;67,213]}; %The establishment and sum of the unit
matrix The general matrices are the same, but there are differences between {} and []
b{3,3};% subscript to display the unit matrix elements
y.x1=34;y.x2=56;
b{3,4}=y;
b (3)=[];%Delete the third element, b{3}=[] means set the third element to an empty matrix
celldisp(b)%Display the entire unit matrix

Guess you like

Origin blog.csdn.net/weixin_56115549/article/details/127009071