Octave学习记录

1.Basic operations

%generate a matrix
a = [1,2,3;4,5,6]
a =

   1   2   3
   4   5   6

a = [1,2,3;4,5,6]; % The end ';' will inhibit the output of assignment operation

%generate a vaector

a = [1 2 3];

a = [1:3];

%specific matrix or vectors

ones(3,1)
zeros(0,1)

a = ones(3,1);
a(1) = 0;
a 

%change or add one row or one column of a matrix

octave:33> a = [1 2 3];
octave:34> a = [a;a]
a =

   1   2   3
   1   2   3

octave:35> a = [a a]
a =

   1   2   3   1   2   3
   1   2   3   1   2   3

octave:36> a = [a a ones(2,1)]
a =

   1   2   3   1   2   3   1   2   3   1   2   3   1
   1   2   3   1   2   3   1   2   3   1   2   3   1

octave:37> a = [a ; ones(1,size(a,2))]
a =

   1   2   3   1   2   3   1   2   3   1   2   3   1
   1   2   3   1   2   3   1   2   3   1   2   3   1
   1   1   1   1   1   1   1   1   1   1   1   1   1

octave:38> a[1,]
parse error:

  syntax error

>>> a[1,]
     ^

octave:38> a[1,:] = zeros(1,size(a,2))
parse error:

  syntax error

>>> a[1,:] = zeros(1,size(a,2))
     ^

octave:38> a(1,:) = zeros(1,size(a,2))
a =

   0   0   0   0   0   0   0   0   0   0   0   0   0
   1   2   3   1   2   3   1   2   3   1   2   3   1
   1   1   1   1   1   1   1   1   1   1   1   1   1

octave:39> a(:,1) = ones(size(a,1),1)
a =

   1   0   0   0   0   0   0   0   0   0   0   0   0
   1   2   3   1   2   3   1   2   3   1   2   3   1
   1   1   1   1   1   1   1   1   1   1   1   1   1


2.Get the maximum value from a matrix

octave:12> a = [1,2,3;4,5,6;7,8,9]
a =

   1   2   3
   4   5   6
   7   8   9

octave:13> max(a) #default by columns
ans =

   7   8   9

octave:14> [value, index] = max(a) #return the largest value of each column and their rows
value =

   7   8   9

index =

   3   3   3

3.Get unique values of a vector

octave:23> a = [1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]
a =

   1   2   3   4   5   1   2   3   4   5   1   2   3   4   5

octave:24> unique(a)
ans =

   1   2   3   4   5

4.Get the number of rows and columns of a mtrix

octave:27> a = [1 2 3 4 5; 1 2 3 4 5 ;1 2 3 4 5]
a =

   1   2   3   4   5
   1   2   3   4   5
   1   2   3   4   5

octave:28> size(a)
ans =

   3   5

octave:29> size(a,1) % number of rows
ans =  3
octave:30> size(a,2) % number of columns
ans =  5
octave:31> a = [1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]
a =

   1   2   3   4   5   1   2   3   4   5   1   2   3   4   5

octave:32> size(a)
ans =

    1   15

5.Get the position of one special value or change one special value to 0

octave:48> a = [ones(5,1);zeros(5,1)];
octave:49> pos_a = find(a==1)
pos_a =

   1
   2
   3
   4
   5

octave:50> a(pos_a,:)
ans =

   1
   1
   1
   1
   1

octave:51> zero_a = find(a==0)
zero_a =

    6
    7
    8
    9
   10

octave:52> a(zero_a,:)
ans =

   0
   0
   0
   0
   0

%------------------------------------------
octave:53> a = [1,2,3,4,5,1,2,3,4,5]
a =

   1   2   3   4   5   1   2   3   4   5

octave:54> a == 1
ans =

   1   0   0   0   0   1   0   0   0   0

octave:55> a == 5
ans =

   0   0   0   0   1   0   0   0   0   1

6.repmat — repeat a given matrix by given number of times in row and column

octave:24> a = ones(3,4)
a =

   1   1   1   1
   1   1   1   1
   1   1   1   1

octave:25> repmat(a,1,2)
ans =

   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1

octave:26> repmat(a,2,2)
ans =

   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1

7.

  1. 9.
CHENTONG
版权声明:本文为博主原创文章,转载请注明出处。
alipay.png WeChatPay.png

CHENTONG

CHENTONG
积微,月不胜日,时不胜月,岁不胜时。凡人好敖慢小事,大事至,然后兴之务之。如是,则常不胜夫敦比于小事者矣!何也?小事之至也数,其悬日也博,其为积也大。大事之至也希,其悬日也浅,其为积也小。故善日者王,善时者霸,补漏者危,大荒者亡!故,王者敬日,霸者敬时,仅存之国危而后戚之。亡国至亡而后知亡,至死而后知死,亡国之祸败,不可胜悔也。霸者之善著也,可以时托也。王者之功名,不可胜日志也。财物货宝以大为重,政教功名者反是,能积微者速成。诗曰:德如毛,民鲜能克举之。此之谓也。

生信宝典文章集锦

生信的作用越来越大,想学的人越来越多,不管是为了以后发展,还是为了解决眼下的问题。但生信学习不是一朝一夕就可以完成的事情,也许你可以很短时间学会一个交互式软件的操作,却不能看完程序教学视频后就直接写程序。也许你可以跟着一个测序分析流程完成操作,但不懂得背后的原理,不知道什么...… Continue reading

生信宝典文章集锦

Published on January 01, 2100

生信宝典文章集锦

Published on January 01, 2100