Path: blob/master/Week 2/Octave Tutorial/3. Computing On Data/computing_on_data.txt
625 views
octave:5> A = [1 2; 3 4; 5 6]1A =231 243 455 667octave:6> B = [10 11; 12 13; 14 15]8B =91010 111112 131214 151314octave:7> C = [1 2 3; 4 5 6]15C =16171 2 3184 5 61920octave:8> A * C21ans =22239 12 152419 26 332529 40 512627octave:9> B * C28ans =293054 75 963164 89 1143274 103 1323334octave:10> A .* B35ans =363710 223836 523970 904041octave:11> V = [1; 2; 3]42V =434414524634748octave:12> V + 149ans =505125235345455octave:13> V' #transpose56ans =57581 2 35960octave:14> (V+1)'61ans =62632 3 46465octave:15> x = (V+1)'66x =67682 3 46970octave:16> max (x)71ans = 472octave:17> [val, ind] = max (x)73val = 474ind = 375octave:18> A76A =77781 2793 4805 68182octave:19> max (A) #returns the column with the max value83ans =84855 68687octave:20> x88x =89902 3 49192octave:21> x < 393ans =94951 0 09697octave:22> find (x<3)98ans = 199octave:23> A100A =1011021 21033 41045 6105106octave:24> [r, c] = find(A>=7)107r = [](0x1)108c = [](0x1)109octave:25> [r, c] = find(A>=3)110r =1111122113311421153116117c =1181191120112121222123124octave:26> D = rand(3,1)125D =1261270.0371381280.0409051290.339827130131octave:27> D = rand(1,1)132D = 0.63497133octave:28> D = rand(1,0)134D = [](1x0)135octave:29> D = rand(1,3)136D =1371380.39812 0.37656 0.65296139140octave:30> floor(D)141ans =1421430 0 0144145octave:31> ceil(D)146ans =1471481 1 1149150octave:32> max (2,1)151ans = 2152octave:33> max (rand(2), rand(2))153ans =1541550.51577 0.711981560.19302 0.56167157158octave:34> rand(2)159ans =1601610.941753 0.4282141620.084452 0.279988163164octave:35> #that was the max of two random matrices of 2x2 dimensions each165octave:35> A = magic(3)166A =1671688 1 61693 5 71704 9 2171172octave:36> max(A, [], 1)173ans =1741758 9 7176177octave:37> max(A, [], 2) #will return the max of each row178ans =179180818171829183184octave:38> max(A, [], 3)185ans =1861878 1 61883 5 71894 9 2190191octave:39> max(A, [], 4)192ans =1931948 1 61953 5 71964 9 2197198octave:40> A = magic (9)199A =20020147 58 69 80 1 12 23 34 4520257 68 79 9 11 22 33 44 4620367 78 8 10 21 32 43 54 5620477 7 18 20 31 42 53 55 662056 17 19 30 41 52 63 65 7620616 27 29 40 51 62 64 75 520726 28 39 50 61 72 74 4 1520836 38 49 60 71 73 3 14 2520937 48 59 70 81 2 13 24 35210211octave:41> sum(A,1)212ans =213214369 369 369 369 369 369 369 369 369215216octave:42> help sum217'sum' is a built-in function from the file libinterp/corefcn/data.cc218219-- sum (X)220-- sum (X, DIM)221-- sum (..., "native")222-- sum (..., "double")223-- sum (..., "extra")224Sum of elements along dimension DIM.225226If DIM is omitted, it defaults to the first non-singleton227dimension.228229The optional "type" input determines the class of the variable used230for calculations. By default, operations on floating point inputs231(double or single) are performed in their native data type, while232operations on integer, logical, and character data types are233performed using doubles. If the argument "native" is given, then234the operation is performed in the same type as the original235argument.236237For example:238239sum ([true, true])240=> 2241sum ([true, true], "native")242=> true243244If "double" is given the sum is performed in double precision even245for single precision inputs.246247For double precision inputs, the "extra" option will use a more248accurate algorithm than straightforward summation. For single249precision inputs, "extra" is the same as "double". For all other250data type "extra" has no effect.251252See also: cumsum, sumsq, prod.253254Additional help for built-in functions and operators is255available in the online version of the manual. Use the command256'doc <topic>' to search the manual index.257258Help and information about Octave is also available on the WWW259at https://www.octave.org and via the [email protected]260mailing list.261octave:43> A262A =26326447 58 69 80 1 12 23 34 4526557 68 79 9 11 22 33 44 4626667 78 8 10 21 32 43 54 5626777 7 18 20 31 42 53 55 662686 17 19 30 41 52 63 65 7626916 27 29 40 51 62 64 75 527026 28 39 50 61 72 74 4 1527136 38 49 60 71 73 3 14 2527237 48 59 70 81 2 13 24 35273274octave:44> sum(A)275ans =276277369 369 369 369 369 369 369 369 369278279octave:45> B = [1 2; 3 4; 5 6]280B =2812821 22833 42845 6285286octave:46> sum(B)287ans =2882899 12290291octave:47> sum(B,1)292ans =2932949 12295296octave:48> sum(B,2)297ans =2982993300730111302303