Contents

% Ponieważ nie można użyć funkcji publish dla skryptów zawierających
% polecenie input skrytp został zmodyfikowany, tak aby uniknąc problemów
% wynikajacych z tego ograniczenia. Linijki zawierające polecenie input
% zostały zastąpione odpowiednim fragmentem, np.:
%   userinput = 123
%   if ~exist('userinput', 'var')
%       userinput = input('Input your input');
%   end
% W celu przetsestowania przykładów zawierających polecenie input należy
% zakomentować linijkę, w które zaincjalizowano zmienną, której wartość ma
% być określona jako wynik polecenia input (w powyższym przykładzie jest to
% linia userinput = 123).

Operatory arytmetyczne - skalary

help ops
  Operators and special characters.
 
  Arithmetic operators.
    plus       - Plus                               +    
    uplus      - Unary plus                         +    
    minus      - Minus                              -    
    uminus     - Unary minus                        -    
    mtimes     - Matrix multiply                    *    
    times      - Array multiply                    .*    
    mpower     - Matrix power                       ^    
    power      - Array power                       .^    
    mldivide   - Backslash or left matrix divide    \    
    mrdivide   - Slash or right matrix divide       /    
    ldivide    - Left array divide                 .\    
    rdivide    - Right array divide                ./    
    idivide    - Integer division with rounding option.
    kron       - Kronecker tensor product   
 
  Relational operators.
    eq         - Equal                             ==     
    ne         - Not equal                         ~=     
    lt         - Less than                          <      
    gt         - Greater than                       >      
    le         - Less than or equal                <=     
    ge         - Greater than or equal             >=     
 
  Logical operators.
    relop      - Short-circuit logical AND         &&     
    relop      - Short-circuit logical OR          ||     
    and        - Element-wise logical AND           &      
    or         - Element-wise logical OR            |      
    not        - Logical NOT                        ~      
    punct      - Ignore function argument or output ~
    xor        - Logical EXCLUSIVE OR
    any        - True if any element of vector is nonzero
    all        - True if all elements of vector are nonzero
 
  Special characters. 
    colon      - Colon                              : 
    paren      - Parentheses and subscripting      ( )              
    paren      - Brackets                          [ ]     
    paren      - Braces and subscripting           { }          
    punct      - Function handle creation           @
    punct      - Decimal point                      .      
    punct      - Structure field access             .      
    punct      - Parent directory                   ..     
    punct      - Continuation                       ...    
    punct      - Separator                          ,      
    punct      - Semicolon                          ;      
    punct      - Comment                            %      
    punct      - Invoke operating system command    !            
    punct      - Assignment                         =
    punct      - Quote                              '      
    transpose  - Transpose                         .'
    ctranspose - Complex conjugate transpose        ' 
    horzcat    - Horizontal concatenation          [,]     
    vertcat    - Vertical concatenation            [;]     
    subsasgn   - Subscripted assignment          ( ),{ },.   
    subsref    - Subscripted reference           ( ),{ },.   
    subsindex  - Subscript index
    metaclass  - Metaclass for MATLAB class         ?
 
  Bitwise operators.
    bitand     - Bit-wise AND.
    bitcmp     - Complement bits.
    bitor      - Bit-wise OR.
    bitxor     - Bit-wise XOR.
    bitset     - Set bit.
    bitget     - Get bit.
    bitshift   - Bit-wise shift.
 
  Set operators.
    union      - Set union.
    unique     - Set unique.
    intersect  - Set intersection.
    setdiff    - Set difference.
    setxor     - Set exclusive-or.
    ismember   - True for set member.
 
  See also ARITH, RELOP, SLASH, FUNCTION_HANDLE.

Opartory arytmetyczne - macierze

A = [1 2; 3 4]
B = [5 6; 7 8]
A + 1
A + B
A * 3
A * B
A \ B
A / B
2^A
A^2
A*A
A =

     1     2
     3     4


B =

     5     6
     7     8


ans =

     2     3
     4     5


ans =

     6     8
    10    12


ans =

     3     6
     9    12


ans =

    19    22
    43    50


ans =

    -3    -4
     4     5


ans =

    3.0000   -2.0000
    2.0000   -1.0000


ans =

   10.4827   14.1519
   21.2278   31.7106


ans =

     7    10
    15    22


ans =

     7    10
    15    22

Operatory arytmetyczne - tablice

X = ones(3,3,3)
Y = X + 5
Z = X + Y
Z = X * 3
% Z = X * Y
W = Z/6
W = Z\6
% W = Z/Y
% W = Z^2
% W = Z^Y
X(:,:,1) =

     1     1     1
     1     1     1
     1     1     1


X(:,:,2) =

     1     1     1
     1     1     1
     1     1     1


X(:,:,3) =

     1     1     1
     1     1     1
     1     1     1


Y(:,:,1) =

     6     6     6
     6     6     6
     6     6     6


Y(:,:,2) =

     6     6     6
     6     6     6
     6     6     6


Y(:,:,3) =

     6     6     6
     6     6     6
     6     6     6


Z(:,:,1) =

     7     7     7
     7     7     7
     7     7     7


Z(:,:,2) =

     7     7     7
     7     7     7
     7     7     7


Z(:,:,3) =

     7     7     7
     7     7     7
     7     7     7


Z(:,:,1) =

     3     3     3
     3     3     3
     3     3     3


Z(:,:,2) =

     3     3     3
     3     3     3
     3     3     3


Z(:,:,3) =

     3     3     3
     3     3     3
     3     3     3


W(:,:,1) =

    0.5000    0.5000    0.5000
    0.5000    0.5000    0.5000
    0.5000    0.5000    0.5000


W(:,:,2) =

    0.5000    0.5000    0.5000
    0.5000    0.5000    0.5000
    0.5000    0.5000    0.5000


W(:,:,3) =

    0.5000    0.5000    0.5000
    0.5000    0.5000    0.5000
    0.5000    0.5000    0.5000


W(:,:,1) =

     2     2     2
     2     2     2
     2     2     2


W(:,:,2) =

     2     2     2
     2     2     2
     2     2     2


W(:,:,3) =

     2     2     2
     2     2     2
     2     2     2

Operatory tablicowe

a = [1 2 3];
b = [4 5 6];
c =  a.*b % c = [a(1)*b(1) a(2)*b(2) a(3)*b(3)]
[2 3 4].^[3 2 1]
sum(a.*b) % Iloczyn skalarny dwóch wektorów
2.^A
A.^2
c =

     4    10    18


ans =

     8     9     4


ans =

    32


ans =

     2     4
     8    16


ans =

     1     4
     9    16

Priorytety

help precedence
1 + 2 * 3
6 / 2 * 3
2 * 2 ^ 3
2 ^ (1 + 2) / 3
1 + 1:5
1 + [1:5]
 PRECEDENCE Operator Precedence in MATLAB.
 
    MATLAB has the following precedence for the built-in operators when
    evaluating expressions (from highest to lowest):
 
       1. transpose (.'), power (.^), complex conjugate
          transpose ('), matrix power (^)
 
       2. unary plus (+), unary minus (-), logical negation (~)
 
       3. multiplication (.*), right division (./), left
          division (.\), matrix multiplication (*), matrix right 
          division (/), matrix left division (\)
 
       4. addition (+), subtraction (-)
 
       5. colon operator (:)
 
       6. less than (<), less than or equal to (<=), greater than
          (>), greater than or equal to (>=), equal to (==), not
          equal to (~=)
 
       7. element-wise logical AND (&)
 
       8. element-wise logical OR (|)
 
       9. short-circuit logical AND (&&)
 
      10. short-circuit logical OR (||)
 
    See also SYNTAX, ARITH.


ans =

     7


ans =

     9


ans =

    16


ans =

    2.6667


ans =

     2     3     4     5


ans =

     2     3     4     5     6

Reprezentacja liczb w oknie poleceń

help format
123
3253235.23
32.2523e-3
[728, 725324547565659]
[225323.23 523.23]
[3.2376 9.2534e-7]
format long
[3.2376 9.2534e-7]
format shortg
[3.2376 9.2534e-7]
format short
format - Set Command Window output display format

    This MATLAB function changes the output display format in the Command Window to
    the format specified by style.

    format style
    format

    See also disp, fprintf, rat


ans =

   123


ans =

   3.2532e+06


ans =

    0.0323


ans =

   1.0e+14 *

    0.0000    7.2532


ans =

   1.0e+05 *

    2.2532    0.0052


ans =

    3.2376    0.0000


ans =

   3.237600000000000   0.000000925340000


ans =

       3.2376   9.2534e-07

Liczbowe typy (klasy) danych

help datatypes
  Data types and structures.
 
  Data types (classes)
    double          - Convert to double precision.
    logical         - Convert numeric values to logical.
    cell            - Create cell array.
    struct          - Create or convert to structure array.
    table           - Create a table from workspace variables.
    single          - Convert to single precision.
    uint8           - Convert to unsigned 8-bit integer.
    uint16          - Convert to unsigned 16-bit integer.
    uint32          - Convert to unsigned 32-bit integer.
    uint64          - Convert to unsigned 64-bit integer.
    int8            - Convert to signed 8-bit integer.
    int16           - Convert to signed 16-bit integer.
    int32           - Convert to signed 32-bit integer.
    int64           - Convert to signed 64-bit integer.
    categorical     - Create a categorical array.
    inline          - Construct INLINE object.
    function_handle - Function handle array.
    javaArray       - Construct a Java Array object.
    javaMethod      - Invoke a Java method.
    javaObject      - Invoke a Java object constructor.
    javaMethodEDT   - Invoke a Java method on the Swing Event Dispatch Thread.
    javaObjectEDT   - Invoke a Java object constructor on the Swing Event Dispatch Thread.
 
    cast            - Cast a variable to a different data type or class.
 
  Class determination functions.
    isnumeric       - True for numeric arrays.
    isfloat         - True for floating point arrays, both single and double.
    isinteger       - True for arrays of integer data type.
    islogical       - True for logical array.
 
    iscom           - true for COM/ActiveX objects.
    isinterface     - true for COM Interfaces.
 
  Cell array functions.
    cell            - Create cell array.
    celldisp        - Display cell array contents.
    cellplot        - Display graphical depiction of cell array.
    cell2mat        - Convert the contents of a cell array into a single matrix.
    mat2cell        - Break matrix up into a cell array of matrices.
    num2cell        - Convert numeric array into cell array.
    deal            - Deal inputs to outputs.
    cell2struct     - Convert cell array into structure array.
    struct2cell     - Convert structure array into cell array.
    iscell          - True for cell array.
 
  Array functions.
    arrayfun        - Apply a function to each element of an array.
    cellfun         - Apply a function to each cell of a cell array.
    structfun       - Apply a function to each field of a scalar structure.
 
  Structure functions.
    struct          - Create or convert to structure array.
    fieldnames      - Get structure field names.
    getfield        - Get structure field contents.
    setfield        - Set structure field contents.
    rmfield         - Remove fields from a structure array.
    isfield         - True if field is in structure array.
    isstruct        - True for structures.
    orderfields     - Order fields of a structure array.
 
  Table functions.
    table           - Create a table from workspace variables.
    array2table     - Convert matrix to table.
    cell2table      - Convert cell array to table.
    struct2table    - Convert structure array to table.
    table2array     - Convert table to a homogeneous array.
    table2cell      - Convert table to cell array.
    table2struct    - Convert table to structure array.
    istable         - True for tables.
 
  Categorical functions.
    categorical     - Create a categorical array.
    iscategorical   - True for categorical arrays.
 
  Function handle functions.
    @               - Create function_handle; use "help function_handle".
    func2str        - Construct a string from a function handle.
    str2func        - Construct a function_handle from a function name string.
    functions       - List functions associated with a function_handle.
 
  Byte manipulation functions.
    swapbytes       - Swap byte ordering, changing endianness.
    typecast        - Convert datatypes without changing underlying data.
 
  Object oriented programming functions.
    class           - Create object or return object class.
    classdef        - Define a new MATLAB class.
    struct          - Convert object to structure array.
    methods         - Display class method names.
    methodsview     - View names and properties of class methods.
    properties      - Display class property names.
    events          - Display class event names.
    enumeration     - Display class enumerated value names.
    superclasses    - Display names of the superclasses of a given class.
    isa             - True if object is a given class.
    isjava          - True for Java object arrays
    isobject        - True for MATLAB objects.
    inferiorto      - Inferior class relationship.
    superiorto      - Superior class relationship.
    substruct       - Create structure argument for SUBSREF or SUBSASGN.
    ismethod        - True if method of an object.
    isprop          - Returns true if the property exists
    metaclass       - Metaclass for MATLAB class
 
    loadobj         - Called when loading an object from a .MAT file.
    saveobj         - Called when saving an object to a .MAT file.

Rzutowanie typów

x = [1 127 256 513]
x_i16 = int16(x)
x_ui8 = uint8(x_i16)
x_ui8c = cast(x_i16, 'uint8')
x =

     1   127   256   513


x_i16 =

      1    127    256    513


x_ui8 =

    1  127  255  255


x_ui8c =

    1  127  255  255

Wartości minimalne i maksymalne dla danego typu

intmax
intmax('int32')
intmax('int64')
intmax('int8')
intmax('uint8')
realmax
realmax('double')
realmax('single')

intmin
intmin('int32')
intmin('int64')
intmin('int8')
intmin('uint8')
realmin
realmin('double')
realmin('single')
ans =

  2147483647


ans =

  2147483647


ans =

  9223372036854775807


ans =

  127


ans =

  255


ans =

  1.7977e+308


ans =

  1.7977e+308


ans =

  3.4028e+38


ans =

 -2147483648


ans =

 -2147483648


ans =

 -9223372036854775808


ans =

 -128


ans =

    0


ans =

  2.2251e-308


ans =

  2.2251e-308


ans =

  1.1755e-38

Rzmiar i liczba elementów macierzy

w2 = [-1 -3 5 1 9 11]
length(w2)
mat_4 = [magic(4) magic(4)]
length(mat_4)
size(mat_4)
help size
smat = size(mat_4)
[w k] = size(mat_4)
W = size(mat_4,1)
K = size(mat_4,2)
numel(mat_4)
mat_4(:)
w2 =

    -1    -3     5     1     9    11


ans =

     6


mat_4 =

    16     2     3    13    16     2     3    13
     5    11    10     8     5    11    10     8
     9     7     6    12     9     7     6    12
     4    14    15     1     4    14    15     1


ans =

     8


ans =

     4     8

size - Array dimensions

    This MATLAB function returns the sizes of each dimension of array X in a vector,
    d, with ndims(X) elements.If X is a scalar, then size(X) returns the vector [1
    1].

    d = size(X)
    [m,n] = size(X)
    m = size(X,dim)
    [d1,d2,d3,...,dn] = size(X),

    See also exist, length, ndims, numel, whos

    Other uses of size
        distcomp/size, matlab/size (serial), matlab/serial.size


smat =

     4     8


w =

     4


k =

     8


W =

     4


K =

     8


ans =

    32


ans =

    16
     5
     9
     4
     2
    11
     7
    14
     3
    10
     6
    15
    13
     8
    12
     1
    16
     5
     9
     4
     2
    11
     7
    14
     3
    10
     6
    15
    13
     8
    12
     1

Liczby zespolone - postać algebraiczna (kanonicza)

sqrt(-1)
z = 2 + 3*i;
z = 2 + 3i
z = 2 + 3j
i = 10
z = 2 + 3*i
z = 2 + 3i
z1 = 10 + 5i;
z2 = 5 + 10i;
ans =

   0.0000 + 1.0000i


z =

   2.0000 + 3.0000i


z =

   2.0000 + 3.0000i


i =

    10


z =

    32


z =

   2.0000 + 3.0000i

Operatory i funkcje dla liczb zespolonych

z1 + z2
z1 - z2
z1 * z2
sqrt(z1)
eps(i*pi)
real(z)
imag(z)
conj(z)
angle(z)
abs(z)
ans =

  15.0000 +15.0000i


ans =

   5.0000 - 5.0000i


ans =

   0.0000e+00 + 1.2500e+02i


ans =

   3.2543 + 0.7682i


ans =

   3.5527e-15


ans =

     2


ans =

     3


ans =

   2.0000 - 3.0000i


ans =

    0.9828


ans =

    3.6056

Funkcja complex

a = [1;2;3;4]
b = [2;2;7;7]
c = complex(a,b)

a = uint8([1;2;3;4])
b = uint8([2;2;7;7])
c = complex(a,b)
a =

     1
     2
     3
     4


b =

     2
     2
     7
     7


c =

   1.0000 + 2.0000i
   2.0000 + 2.0000i
   3.0000 + 7.0000i
   4.0000 + 7.0000i


a =

    1
    2
    3
    4


b =

    2
    2
    7
    7


c =

    1 +    2i
    2 +    2i
    3 +    7i
    4 +    7i

Wykres koła - postać wykładnicza liczby zepolonej

kolo = exp(2i*[1:360]*pi/360);
plot(kolo)
axis('equal')

Transpozycja macierzy zepsolonych

a = [1+1i 2+2i 3+3i; 4+4i 5+5i 6+6i]
a' % sprzężenie hermitowskie
a.'
a =

   1.0000 + 1.0000i   2.0000 + 2.0000i   3.0000 + 3.0000i
   4.0000 + 4.0000i   5.0000 + 5.0000i   6.0000 + 6.0000i


ans =

   1.0000 - 1.0000i   4.0000 - 4.0000i
   2.0000 - 2.0000i   5.0000 - 5.0000i
   3.0000 - 3.0000i   6.0000 - 6.0000i


ans =

   1.0000 + 1.0000i   4.0000 + 4.0000i
   2.0000 + 2.0000i   5.0000 + 5.0000i
   3.0000 + 3.0000i   6.0000 + 6.0000i

Łańcuchy znakowe

Znaki i ich kodowanie

numequiv = double('a')
numequiv = int32('a')
char(numequiv)
numequiv =

    97


numequiv =

          97


ans =

a

Działania arytmetyczne na znakach

numequiv = double('a');
char(numequiv + 1)
'a' + 2
char('a' + 2)
ans =

b


ans =

    99


ans =

c

Tworzenie łańcuchów znakowych

word = 'cat'
% strvar = input('Wprowadź łańcuch znakowy: ', 's')
strvar = 'lancuch znakowy'
if ~exist('strvar', 'var')
    strvar = input('Wprowadź łańcuch znakowy: ', 's');
end
word =

cat


strvar =

lancuch znakowy

length('cat')
length(' ')
length('')
ans =

     3


ans =

     1


ans =

     0

Macierze łańcuchów znakowych

wordmat = ['Hej, halo!'; 'Jak leci ?']
size(wordmat)
wordmat(2,8)
wordmat(1,:)
wordmat =

Hej, halo!
Jak leci ?


ans =

     2    10


ans =

i


ans =

Hej, halo!

double('abcd')
char('abcd' + 1)
ans =

    97    98    99   100


ans =

bcde

Konkatenacja łańcuchów znakowych

imie = 'Jan'
nazwisko = 'Kowalski'
[imie nazwisko]
imie =

Jan


nazwisko =

Kowalski


ans =

JanKowalski

strcat(imie, nazwisko)
ans =

JanKowalski

str1 = 'xxx   '
str2 = '   yyy'
length(str1)
length(str2)
[str1 str2]
length(ans)
str1 =

xxx   


str2 =

   yyy


ans =

     6


ans =

     6


ans =

xxx      yyy


ans =

    12

strcat(str1, str2)
length(ans)
strcat(str2, str1)
length(ans)
ans =

xxx   yyy


ans =

     9


ans =

   yyyxxx


ans =

     9

strvcat(imie, nazwisko)
size(ans)
ans =

Jan     
Kowalski


ans =

     2     8

Konwersja liczb zapisanych jako typ znakowy do typu liczbowego

rani = randi(50,1,1)
s1 = int2str(rani)
length(rani)
length(s1)
rani =

    42


s1 =

42


ans =

     1


ans =

     2

str3 = num2str(3.456789,3)
length(str3)
str4 = num2str(3.456789,'%6.4f')
length(str4)
str3 =

3.46


ans =

     4


str4 =

3.4568


ans =

     6

randMat = rand(2,3)*100
randMatStr = num2str(randMat,3)
randVecStr = num2str(randMat,'%3.5e\n')
randMat =

   69.4829   95.0222   43.8744
   31.7099    3.4446   38.1558


randMatStr =

69.5        95      43.9
31.7      3.44      38.2


randVecStr =

6.94829e+01
9.50222e+01
4.38744e+01
3.17099e+01
3.44461e+00
3.81558e+01

num = str2num('123.456')
num =

  123.4560

mystr = '66 2 111'
mynumvec = str2num(mystr)
sum(mynumvec)
mystr =

66 2 111


mynumvec =

    66     2   111


ans =

   179

Funkcje is dla zmiennych typu char i łańcuchów znakowych

isletter('a')
isletter('EK127')
isspace('a b')
ischar('EK127')
ischar(3:5)
ans =

     1


ans =

     1     1     0     0     0


ans =

     0     1     0


ans =

     1


ans =

     0