============================= ===== prace s poli ========== ============================= ### Priklad 1 ----- je dan vektor: ------ v = [ 2 4 -3 0 5 0 -1 -1 1] -- soucet prvku vektoru s = sum(v) -- maximum, minimum --- mx = max(v); mn = min(v); -- pocet zapornych cisel --- zv = (v<0) % jednicky na mistech zap. cisel z = sum(zv) -- pocet nenulovych cisel --- n = sum(v~=0) -- indexy nenulovych cisel nv = find(v) % indexy nenul. cisel n = length(nv) % jejich pocet -- indexy zapornych cisel zv = find(v<0) -- vypsani vsech zapornych cisel ve v zv = find(v<0) zap = v(zv) -- vynulovani vsech zapornych cisel ve v zv = find(v<0) v(zv) = 0 ================================ ### Priklad 2 ----- je dano pole (matice): ------ A = [ 2 3 0 -1 0 -2 1 3 ] -- soucty sloupcu, radku, soucet vsech prvku ss = sum(A); sr = sum(A,2); s = sum(sum(A)); -- maxima ve sloupcich, radcich, celkove maximum (analog. minimum) --- mxs = max(A); mxr = max(A')'; mx = max(max(A)); -- pocty zapornych cisel ve sloupcich a celkem --- zA = (A<0) % jednicky na mistech zap. cisel zs = sum(zA); z = sum(sum(zA)) -- pocet nenulovych cisel --- n = sum(sum(A~=0)) -- pocet nenulovych cisel ve druhem radku --- n = sum(A(2,:)~=0) -- indexy nenulovych cisel iA = find(A) % indexy nenul. cisel (pocitano po sloupcich) [ir, is] = find(A) % indexy radku a sloupcu s nenul. c. -- indexy zapornych cisel zA = find(A<0) -- vypsani vsech zapornych cisel v A zA = find(A<0) zap = A(zA) -- vynulovani vsech zapornych cisel v A zA = find(A<0) A(zA) = 0 ================================ ### Priklad 3 ----- skladani matic ------ A = [ 3 5 -1; 1 -4 2] B = [5 3; 8 4] C = [ 1 2 3 4 5] D=[ A B ] E = [D; C] ================================ ### Priklad 4 ----- je dana ctvercova matice: ------ A = [ 3 5 -1; 1 -4 2; 0 -1 1] ----- rozlozte ji na soucet A = L + D + U, kde --- ----- D - diagonalni, L - dolni trojuh., U - horni tr. --- D = diag(diag(A)) L = tril(A,-1) U = triu(A,1) ==================================