Octave Syntax _octave

Source: Internet
Author: User
Tags clear screen print format rand
Vector

Semicolon: Split Line

Spaces or commas: Split columns Create and access row vectors

Space or comma split

>> v = [1 2 3]% equivalent: v = [1, 2, 3]
v =

   1 2 3
   
>> V (2)% only one row, so specify is the column
ans =  2
Column vectors

Semicolon Split

>> v = [1; 2; 3]
v =

   1
   2
   3

>> V (2)% only one column, so the designation is line
ans =  2
Matrix

Like a vector, a space or comma splits a column, a semicolon splits a row

Colon: represents all, all rows, or all columns created

>>   A = [1, 2; 3, 4]% write
a = 1 2 3 4

>> a = [1, 2;% Branch Write
> 3, 4]< C22/>a =

   1   2
   3   4
Creating tips

A:C---from A to C

A:B:C---from a, interval B, to C

Vectors can also be created in such a way that

>> a = [1:3; 4:6]
a =

   1   2   3
   4   5   6
   
>> a = [1:2:5; 2:2:6]
a =
  1   3   5
   2   4   6
Special matrix Unit Matrix
>> Eye (3)% Unit matrix
ans =

diagonal Matrix

   1   0   0
   0

1 0 0 0 1 >> flipud (Eye (3))
ans =

permutation Matrix

   0   0   1
   0   1 0 1 0   0
Transpose matrix
A =

   1   2
   3   4

>> A '
ans =

   1   3   -2 4
Inverse matrix
A =

   1   2
   3   4

>> PINV (a)
ans =

  -2.00000   1.00000
   1.50000  - 0.50000

>> PINV (a) *
ans =

   1.00000   0.00000
  -0.00000   1.00000
All 1 matrices
>> ones (2, 3)
ans =

   1   1   1   1 1 1
All 0 matrices
>> 0*ones (2, 3)
ans =

   0   0 0 0 0 0
>> Zeros (2, 3)
ans =

   0   0   0
   0   0   0
Random matrices
>> rand (1, 3)% 0~1 random number, 1 Rows 3 column
ans =

   0.99291   0.65946   0.95102
Gauss Distribution Matrix
>> Randn (1, 3)% Gauss distribution
ans =

   0.14646   2.02587   1.33266
Access

Colon: represents all, all rows, or all columns access elements

A =

   1   2   3 4 5 6 7 8 9
   
>> A (2, 2)% second row, second column, first set row and then order
ans =  5
Access single or single column
>> A (1,:)% of the first line, all, elements
ans =

   1   2   3
   
>> A (:, 2)% of the second column, all,
ans =

   2
   5< C20/>8
accessing multiple rows or columns
>> A (:, [1, 3])% of the first column and the third column, all, elements
ans =

   1 3 4 6 7 9

>> A ([1, 3],:)% Row and third row, all, elements
ans =

   1   2   3
   7   8   9
Connection

c = [a b], add B by column to a, generate C

C = [A; b], add B by row to a, build C add element

>> a = [1]
a =  1

>> a = [A, 2]% 2, by column, add to a, then assign to a =

   1   2
   
>> a = [A; 3]% A has two columns, 3 has only one column, the scale does not match
error:vertical dimensions mismatch (1x2 vs 1x1)
Add a row or column
>> a = [A; [3, 4]] % the vector [3, 4], as a row, is added to a, which is assigned to a a
=

   1   2
   3   4
   
>> A = [A, [5; 6]]% to the vector [5, 6], as a column, to a, in the assigned to the a< C50/>a =

   1   2   5
   3   4   6
Matrix Connection
>> a = [1, 2; 3, 4]
A = 1 2 3 4

>> B = [5, 6; 7, 8]
B =

   5   6
   7   8
  >> [A; b]% will B, as row, added to a on
ans =

   1   2
   3   4
   5   6 7 8
All the data into a vector
>> a
a =

   1   2
   3   4

>> A (:)
ans =

+ 1 3 2 4 >> A (:) '
ans =

   1   3   2   4
assigning values

On the basis of access, given data of the same size

A =

   1   2   3 4 5 6 7 8 9

>> A (3, 3) = 10 Modify the value of a single element
a =

    1    2    3
    4 5 6 7 8

>> A (1,:) = [0, 0, 0]% modify a row's value
a =< C60/>0    0    0
    4    5    6
    7    8

>> A (2:3, 2:3)
ans =

    5    6
    8

>> A (2:3, 2:3) = [0, 0; 0, 0]% modifies the value of the specified matrix
a =

   0   0 0 4 0 0< C83/>7   0   0
Operation Plus, minus
>> a = [1 1; 1 1]
A = 1 1 1 1

>> b = [2 2; 2 2]
B =

   2

2 2-2 >> A + B
ans =

   3   3
   3   3

>> a-b
ans =

  -1  -1
  -1  -1< C23/>>> A-1
ans =

  0  0
  0  0
Multiply
>> a = [1 2; 3 4]
A = 1 2 3 4

>> b = [5 6; 7 8]
B =

   5   6
   7
  8

>> A * B
ans =

>>-A%-1 *
ans =

  -1  -2
  -3  -4
Point operation

corresponding element operations

Same dimension: multiplication of corresponding elements

Row dimensions are the same: multiply each row of corresponding elements

Same as Levi: multiply the corresponding elements of each column

A. * b = b. * A

In addition to/; The equal dimensions of the square ^;

A =

   1   1
   1   1

b =

   2   3 2 3

>> A. *
ans =

   2   3
   2   3
Same row dimension
A =

   1   1
   1   1

b =

   5   6

>> A. *
ans =   + 5 6 5 6
Same as the levy degree
A =

   1   1
   1   1

b =

   5
   6

>> A. *
ans =   + 5 5 6 6
Point in addition

Matrix times constant, A * 2, except can be, A/2

Conversely, 2 * A is OK, 2/a is no good, want to use 2./A

A =

   1   2
   3   4

>> 1./a
ans =

   1.00000   0.50000
   0.33333   0.25000
Logic

Each element makes a comparison, marking 0 or 1

&GT,!= (or ~=), &&

A =

   1   2
   3   4

>> A > 2
ans =

   0   0   1 1
Bit operations

or | , Function xor

and &

Non ~

XOR or ^ control statements if

i =  1
>> if i = =
1     > Disp (1)
>  ElseIf i = 2
>     disp (2)
>  Else
>     disp (3)
>  End
For
>> for i = 1:3 from 1 to 3
>     disp (i)
> End
 1
 2
 3
While
>> while I <= 3
>     disp (i)
>     i = i + 1
>  end
 1
 2
 3
Break,continue

No difference function with C, C + +, Java

Size: Getting the Matrix dimension

Length: Get maximum dimension

WHO: Variable list

Whos: Variable details

Clear variable Name: Delete specified variable

Clear: Delete all variables

Find: Returns the subscript that matches the conditional element

Log:log to the end of E

Exp:e How many times the party

ABS: Absolute Value

Floor: Rounding Down

Ceil: Rounding up

Sum: Sum

Prop: Quadrature size

Get The Matrix dimension

A =

   1   2
   3   4
   5   6
>> asize = Size (a)
asize =

   3   2
   
>> Size (A, 1)% 3 line
ans =  3

>> Size (A, 2)% 2 column
ans =  2
Length

Get maximum dimension

A =

   1   2
   3   4
   5   6

>> length (a)% output max dimension
ans =  3
W.H.O.

Variable list

>> who% now have where variable Variables in the current
scope:

A          asize      ans        featuresx  W
Whos

Variable details

>> whos% variable details Variables in the current
scope:

   Attr Name           Size                     Bytes  Class
   = = = = =           = =                     =====  =====
        A              3x2  double
        asize          1x2  Double
        ans            1x20  char
        featuresx     27x2                        432  Double
        W              1x10000                  80000  Double Total is

10082 elements using 80516 bytes
Clear

Delete a variable

>> Clear Featuresx >> who Variables in the current
scope:

A      asize  ans    w
>> Clear% clears all variables
>> who% one variable none.
Find

Returns the subscript that conforms to the criteria element

A =

   5   6
   7   8

>> [R, c] = find (A > 6)% meet the requirements: The second line of the first and second
r =

   2
   2

c =

   1
   2
Sum

Sum

>> a = [1, 2; 3, 4]
A = 1 2 3 4

>> sum (a)% equivalent: Sum (A, 1)
ans =

   4   6
   
>> sum (A, 2)
ans =

   3
   7
Prod

Quadrature

>> a
a =

   1   2
   3   4

>> prod (a)% equivalent: Prod (A, 1)
ans =

   3   8

>> prod (A, 2)
ans =

    2
   12
Max Vector
>> a = [1 5 2 3.3]
a =

   1.0000   5.0000   2.0000   3.3000

>> Max (a)
ans =  5

>> [val, Ind] = max (a)
val =  5
ind =  2
Matrix comparison

Two matrices compare each element, keep the large

>> A = rand (3)
A =

   0.2620788   0.6346345   0.4659161
   0.0880455   0.1258945   0.0079559
   0.0296765   0.7917592   0.4321800

>> b = rand (3)
B =

   0.039237   0.672424   0.214649
   0.491320   0.362929   0.197626
   0.821090 0.675265 0.698960

>> Max ( A, B)
ans =

   0.26208   0.67242   0.46592
   0.49132   0.36293   0.19763
   0.82109   0.79176   0.69896
Maximum row and column values

Parameter two: The Matrix to compare with
Parameter three: By Rows or by column

>> a = [1 2; 3 4]
A = 1 2 3 4

>> Max (A)% maximum per column
ans =

   3   4

& Gt;> Max (A, [], 1)%
ans =

   3   4

>> Max (A, [], 2)%
ans =

   2
   4 per row max
Randperm

Generate a sequence of random sequences

>> a = [2, 3, 4, 5, 6]
A =

   2 3 4 5 6
>> rand_indices = randperm (Length (a))
rand_indices =

   1   2   4   5   3
>> A (:, Rand_indices (1:3))
ans =

   5   6   2
Custom Function Creation Step

Create the file that holds the function (the filename is the same as the function name,. m ends)

Square.m

Create a function

% A return value
function return value = function name (argument list)
    function body
end

% multiple return value function
[return value 1, return value 2] = function name (argument list)
    function body
end
Function y = Square (x)
  y = x^2;
End

function [Y1, y2] = squareandcube (x)
  y1 = x^2;
  y2 = x^3;
End
Call

Enter the directory where the function file resides

Add the directory that holds the function file to, search the path

>> CD downloads/% Enter the directory where function files are stored
>> Square (2)
ans =  4

>> addpath (' ~/downloads ')% Add to search Path
>> CD.
>> Square (2)
ans =  4
Data path

As with Linux commands

>> pwd% Current path
ans =/users/xxx

>> cd ~/developer

>> pwd
ans =/users/xxx/developer

>> ls% list directory files and folders

>> CDs.% Back to superior directory
Load and save
>> load featuresX.dat% load data, variable named Featuresx
>> data = Load (' ex1data1.txt ');% variable named data

>> save F Eaturesx.mat Featuresx; % save data in Featuresx to Featuresx file
>> save Hello.txt v-ascii% The data in variable v is encoded in ASCII mode to Hello.txt
Other commands

Simplify the command line: PS1 (' >> ');

Close the chart: closed or closing all

Command line Clear screen: CLC annotation

% Print Auto Print for comments

A = 3 will print data
a = 3;% will not print data, semicolons can prevent output
Formatting
Disp (PI)% output: 3.1416
disp (sprintf (' pi is%.2f ', pi))% C language style
Default format
Format long% modify default print format
Help
Help size% Look at the assistance document for the size function Help for
Figure Plot Properties

LineWidth: line width

Markerfacecolor: Mark Color

Markersize: Mark Size X,y axis

Ylabel (' x ');
Xlabel (' Y '); Histogram

>> W =-6 + sqrt *randn (1, 10000);
>> hist (w)
>> hist (w, 30)% 30 Group
from:https://segmentfault.com/a/1190000004204177

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.