octave向量矩陣基本操作命令

筆記總結自coursera的機器學習課程~
懶得中英文切換,就用我幼稚有限的英語寫啦,反正~嗯哼嗯哼,這麼簡單的英語,以後我用到也一定看得懂 (- ^ -)
命令如下:

  • At first, we need to know, “;” means turn to the next line of the matrix in octave
  • a=[1 2; 3 4; 5 6] # a is a 3*2 matrix. If you do not want octave to print it out, add ; to the sentence
  • help functionName # print the docum about the function
  • hist() # draw a picture
  • size(a) # return a vector expressing the row and column of matrix a: row * column
  • size(a, 1) # give the row of matrix a
  • length(a) # usually a is a vector, and it return the number of vector a’s elements
  • pwd # show the current directories or the current path that octave is in
  • ls # list the directories
  • load fileName # load the file ( usually we should first enter where the file is with “cd” command)
  • who # show the variables created in octave
  • whos # show more details about the variables
  • clear name # used to clear variables, if name is empty, then clear all
  • load housePrice.txt # load a file named houePrice.txt
  • v=housePrice(1:10) # take the first ten elements from housePrice matrtx to v
  • save hello.mat v # save the vector v to a file called hello.mat
  • save hello.txt v -ascii # if you want to save it more readable, then save it to .txt using ascii format
  • if a is a matrix:
  • a(3,2) # 3, 2 are used as the row and column in the matrix a to take the element
  • a(3, :) # take the third row of a as a vector
  • a( : ) # take all the elements into a single column vector
    eg : a=[1 2; 3 4] -> a( : ) -> a=[1; 2; 3; 4]

  • a([1, 3], : ) # take every thing in the first and the third row as the column isnnot limited

  • a( : , 2) = [1 2] # replace the second column with vector [1 2]
  • a = [a, [1; 2; 3]] # append another column [1, 2, 3] to a as the finally column

  • supported that a is 2 by 2 matrix, and the same to b

  • c=[a b] # takes these two matrix and concatenating onto each other, in a word, matrix a is on the left and b is on the right. c will be a 3 by 6 matrix
  • c =[a; b] # matrix b is on the bottom of a. c will be a 6 by 3 matrix

    so we strengthen our concept that “;” semicolon always means put the next at the bottom and follow, we are going to know the “.” dot, which means the so-called element-wise operation. What is element-wise operation? It means the operation is put is every element of the matrix. For example, a = [1 2; 3 4], after a.^2, we get a = [1 4; 9 16]

  • a.*c # elements in a multiply the corresponding element in matrix c
  • 1./a # elements in a are divided by 1
  • eye(4) # create a matrix whose elements in the diagonal are 1, and all the other is 0
  • log(a)
  • abs(a)
  • -a # the same as -1 * a
  • a’ # transpose a, so (a’)’ will return a itself
  • max(a) # if a is a vector, return the only biggest number, if it is a matrix, return a row
  • [val, index] = max(a) # val will be the maximum value and index will be its index
  • supposed that c is vector like [1, 18, 3, 4 ]
  • c < 5 # return [1, 0, 1, 1] if element is smaller than the given, get 1
  • find(c < 5) # return the index of elements in the matrix that smaller than 5
  • [r, c] = find(c<5) # r and c will separately represents the row, the column of the correct elements
  • sum(a) # if a is vector, then sum up all the elements into one result; if it is a matrix, then add the elements in the same column, and we finally get a vector
  • sum(a, 1) # column sum, add up the elements in the same column
  • sum(a, 2) # row sum, add up the elements in the same row
  • prod(a) # a:vector, then multiply all the elements to a result; If not, multiply all the element in the same column
    取近似值
  • floor(a) # rounds down the elements of a, so 0.5 gets rounded down to 0
  • ceil(a) #round up to find the bigger and the nearest number
  • pinv(a) # inverse a (take the row to the same order column)
  • magric(3) # create a 3 by 3 matrix that all the rows add up to the same value, the same to the column and the elements in the diagonals
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章