zzm99


  • Home

  • Tags

  • Categories

  • Archives

  • Search

基本操作与矩阵运算

Posted on 2019-07-31 | In matlab |
Words count in article: 1k | Reading time ≈ 6

加减乘除优先级:

  • ^(幂) > *, / > + -

练习:

1

1

1

1

1

MATLAB Calling Priority

1

1

Numeric Display Format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
>> format short
>> pi

ans =

3.1416

>> format long
>> pi

ans =

3.141592653589793

>> format shortE
>> pi

ans =

3.1416e+00

>> format longE
>> pi

ans =

3.141592653589793e+00

>> format bank
>> pi

ans =

3.14

>> format hex
>> pi

ans =

400921fb54442d18

>> format rat
>> pi

ans =

355/113

Command Line Terminal

  • 方向键向上(PgUp)可以展示上一条指令

  • clc: clear command window display

  • clear: remove all variables in the workspace

  • who: variables in the workspace

  • whos: variable information of the workspace

Array(Vector and Matrix)

  • Row vector:

a = [1 2 3 4]

  • Column vector:

b = [1;2;3;4]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
>> a = [1 2 3 4]

a =

1 2 3 4

>> b = [1; 2; 3; 4]

b =

1
2
3
4

>> a * b

ans =

30

>> b * a

ans =

1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16

>>

Array Indexing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
>> A = [1 21 6; 5 17 9; 31 2 7]

A =

1 21 6
5 17 9
31 2 7

>> A(8)

ans =

9

>> A(2)

ans =

5

>> A([1 3 5])

ans =

1 31 17

>> A([1 3; 1 3])

ans =

1 31
1 31

>> A(3, 2)

ans =

2

>> A([1 3],[1 3])

ans =

1 6
31 7

>> A([1 2 3],[1 3])

ans =

1 6
5 9
31 7

>>

Colon operator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
>> B = 1 : 5

B =

1 2 3 4 5

>> B = 1:2:5

B =

1 3 5

>> B = [1:5; 2:3:15; -2:0.5:0]

B =

1.0000 2.0000 3.0000 4.0000 5.0000
2.0000 5.0000 8.0000 11.0000 14.0000
-2.0000 -1.5000 -1.0000 -0.5000 0

>> str = 'a':2:'z'

str =

'acegikmoqsuwy'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>> A = [1 21 6; 5 17 9; 31 2 7]

A =

1 21 6
5 17 9
31 2 7

>> A[3, :] = []
A[3, :] = []
↑
错误: 表达式无效。调用函数或对变量进行索引时,请使用圆括号。否则,请检查不匹配的分隔符。

>> A(3,:) = []

A =

1 21 6
5 17 9

>>

Array Concatenation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
>> A = [1 2; 3 4]

A =

1 2
3 4

>> B = [9 9; 9 9]

B =

9 9
9 9

>> C = [5 6 7 8]

C =

5 6 7 8

>> D = [-2:1]

D =

-2 -1 0 1

>> F = [A ,B; C ; D]

F =

1 2 9 9
3 4 9 9
5 6 7 8
-2 -1 0 1

>>

Array Manipulation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
>> A = [1:3; 4 5 4; 9 8 7]

A =

1 2 3
4 5 4
9 8 7

>> B = [3 3 3 ;2 4 9; 1 3 1]

B =

3 3 3
2 4 9
1 3 1

>> a = 2

a =

2

>> A+a

ans =

3 4 5
6 7 6
11 10 9

>> A/a

ans =

0.5000 1.0000 1.5000
2.0000 2.5000 2.0000
4.5000 4.0000 3.5000

>> A./a

ans =

0.5000 1.0000 1.5000
2.0000 2.5000 2.0000
4.5000 4.0000 3.5000

>> A^a

ans =

36 36 32
60 65 60
104 114 108

>> A.^a

ans =

1 4 9
16 25 16
81 64 49

>> A'

ans =

1 4 9
2 5 8
3 4 7

>> A+B

ans =

4 5 6
6 9 13
10 11 8

>> A*B

ans =

10 20 24
26 44 61
50 80 106

>> A.*B

ans =

3 6 9
8 20 36
9 24 7

>> A/B

ans =

0.0714 0.2857 0.2143
1.1667 0 0.5000
3.2619 -0.2857 -0.2143

>> A./B

ans =

0.3333 0.6667 1.0000
2.0000 1.2500 0.4444
9.0000 2.6667 7.0000

>>

Some Special Matrix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
>> linspace(0, 13, 6)

ans =

0 2.6000 5.2000 7.8000 10.4000 13.0000

>> eye(3)

ans =

1 0 0
0 1 0
0 0 1

>> zeros(2,3)

ans =

0 0 0
0 0 0

>> ones(2,3)

ans =

1 1 1
1 1 1

>> diag([2 3 4])

ans =

2 0 0
0 3 0
0 0 4

>> rand(5)

ans =

0.8147 0.0975 0.1576 0.1419 0.6557
0.9058 0.2785 0.9706 0.4218 0.0357
0.1270 0.5469 0.9572 0.9157 0.8491
0.9134 0.9575 0.4854 0.7922 0.9340
0.6324 0.9649 0.8003 0.9595 0.6787

>>

Some Matrix Related Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
>> A = [1 2 3 ; 0 5  6; 7 0 9]

A =

1 2 3
0 5 6
7 0 9

>> max(A)

ans =

7 5 9

>> max(max(A))

ans =

9

>> sort(A)

ans =

0 0 3
1 2 6
7 5 9

>> sortrows(A)

ans =

0 5 6
1 2 3
7 0 9

>> min(A)

ans =

0 0 3

>> size(A)

ans =

3 3

>> sum(A)

ans =

8 7 18

>> length(A)

ans =

3

>> mean(A)

ans =

2.6667 2.3333 6.0000

>> find(A)

ans =

1
3
4
5
7
8
9

>>

leetcode-621-Task Scheduler

Posted on 2019-07-25 | In leetcode , top-100-liked-questions |
Words count in article: 240 | Reading time ≈ 1

Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.

However, there is a non-negative cooling intervaln that means between twosame tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.

You need to return the least number of intervals the CPU will take to finish all the given tasks.

Example 1:

1
2
3
Input: tasks = ['A','A','A','B','B','B'], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.

Note:

  • The number of tasks is in the range [1, 10000].
  • The integer n is in the range [0, 100].
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int leastInterval(vector<char>& tasks, int n)
{
vector<int> tmp(26,0);
int m=tasks.size();
for(int i=0;i<m;i++)
{
tmp[tasks[i]-'A']++;
}
sort(tmp.begin(),tmp.end());
int val=tmp[25];
int count=0;
int i=25;
while(val==tmp[i] && i>=0)
{
count++;
i--;
}
int res=(n+1)*(val-1)+count;
return max(res,m);
}
};

leetcode-309-Best Time to Buy and Sell Stock with Cooldown

Posted on 2019-07-25 | In leetcode , top-100-liked-questions |
Words count in article: 601 | Reading time ≈ 3

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

  • You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  • After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

Example:

1
2
3
Input: [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]

首先,我们看到问题中提到了三种状态buy、sell和cooldown,那么我们脑中的第一个想法就是通过动态规划来解

如果我们index:i天为冷冻期,那么只能说明index:i-1天卖掉了股票,那么i天的收益和i-1天是一样的

cooldown[i]=sell[i-1]
如果我们考虑index:i天卖出,要求利润最大的话。那么无非是index:i-1之前就持有股票,所以index:i-1天也可以卖出,或者另一种情况就是index:i-1当天买入了股票。

sell[i]=max(sell[i-1], buy[i-1]+prices[i])
如果我们考虑index:i天买入,要求利润最大的话。无非是index:i-1之前就不持有股票,我们要考虑哪天买,所以index:i-1也可以买入,或者另一种可能就是index:i-1天是冷冻期。

buy[i]=max(buy[i-1], cooldown[i-1]-prices[i])
我们第一天不可能卖出或者冻结,那么这两个sell[0]=0 cooldown[0]=0,但是我们第一天可以买入啊,所以buy[0]=-prices[0]。还有一点要注意的就是,我们一定是最后一天卖出或者最后一天冻结利润最大。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (!prices.size())
return 0;
vector<int> buy(prices.size(), 0), sell(prices.size(), 0), cooldown(prices.size(), 0);
buy[0] = -prices[0];
for (unsigned int i = 1; i < prices.size(); ++i)
{
cooldown[i] = sell[i - 1];
sell[i] = max(sell[i - 1], buy[i - 1] + prices[i]);
buy[i] = max(buy[i - 1], cooldown[i - 1] - prices[i]);
}
return max(sell[prices.size() - 1], cooldown[prices.size() - 1]);
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int maxProfit(int[] prices) {
if(prices.length == 0)
return 0;
int[] buy = new int[prices.length];
int[] sell = new int[prices.length];
int[] cooldown = new int[prices.length];
buy[0] = -prices[0];
for(int i=1; i<prices.length; i++)
{
cooldown[i] = sell[i-1];
sell[i] = Math.max(sell[i-1], buy[i-1]+prices[i]);
buy[i] = Math.max(buy[i-1], cooldown[i-1]-prices[i]);
}
return Math.max(cooldown[prices.length-1], sell[prices.length-1]);
}
}

jiqixuexi5

Posted on 2019-07-25 | In 机器学习 |
Words count in article: 0 | Reading time ≈ 1

jiqixuexi4

Posted on 2019-07-25 | In 机器学习 |
Words count in article: 0 | Reading time ≈ 1

多变量线性回归

Posted on 2019-07-25 | In 机器学习 |
Words count in article: 432 | Reading time ≈ 1

多特征(Multiple features)

本节将两个特征(其中x0=1)延伸到多个特征(变量),“特征”看来就是自变量、影响因子,比如价格是因变量,房屋面积、房间数、层数、房龄等是自变量或影响因子。

1

多元线性回归

1

多元梯度下降法(Gradient descent for Multiple variables)

多元线性回归的梯度下降的方法,分别对不同的参数进行偏导数。

1

梯度下降法:特征缩放(Featrue Scaling)

特征缩放:瘦长的椭圆,会导致趋向最值时梯度下降的震荡;所以需要缩放特征值,使得其取值范围相近。

缩放的特征值尽量在-1到1之间,当然,范围在-3到3之间,或者-1/3到1/3之间也是可以接受的。

1

1

均值归一化,(特征值-均值)/标准差,其中,标准差为最大值减去最小值。

1

梯度下降法:学习率(Learning Rate)

学习率选择时,学习率太小,收敛过慢;学习率太大,代价函数与步数之间的曲线可能不是下降,甚至无法收敛。

那么,如何选择学习率呢?一般按照3的倍数去取,如…….0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1……….

1

正规方程(Normal equation)

可以一步得到回归问题的最优解,不需要进行特征缩放,适用于特征量不是很大的时候,一般不适用特别复杂的函数。

1

正规方程的具体公式

1

什么时候使用正规方程,什么时候使用梯度下降?

一般情况下,n<10000时,使用正规方程;当n>10000时,就需要考虑使用梯度下降法。

1

单变量线性回归

Posted on 2019-07-23 | In 机器学习 |
Words count in article: 678 | Reading time ≈ 2

假设函数:抽象出来的数据模型,含有大量的参数变量
代价函数:用来计算使假设函数和实际值差距最小的方法。进而确定假设函数中参数变量的值。

代价函数

目标:寻找一个theta_1, theta_0 使得预测值接近训练集中的样本

方法:预测值与ground truth求差的平方,对于所有样本的该值进行求和,最后除以2m(1/2为简化计算,后面有求导2*1/2变1),形成代价函数。最小化该代价函数,得到对应的theta_0和theta_1.

平方误差——解决回归问题的最常用手段

Read more »

初识机器学习

Posted on 2019-07-23 | In 机器学习 |
Words count in article: 422 | Reading time ≈ 1

什么是机器学习?

机器学习的定义:

  1. 在没有明确设置的情况下,使计算机具有学习能力——Samuel
  2. 计算机程序从经验E中学习,解决某一任务T,进行某一性能度量P,通过P测定在T上的表现因经验E而提高——Tom Mitchell(experience,task,performance measure)

机器学习算法分类:

  1. 监督学习(Supervised learning)
  2. 无监督学习(Unsupervised learning)
  3. 强化学习(Reinforcement learning)
  4. 推荐系统(Recommender systems)

监督学习

监督学习是指 我们给算法一个数据集,其中包含了正确答案。也就是说我们给它一个房价数据集,在这个数据集中的每个样本,我们都给出正确的价格即这个房子实际卖价,算法的目的就是给出更多的正确答案,例如为你朋友想要卖掉的这所新房子给出估价。

  • 回归问题:结果是线性的(我们设法预测出一个连续值的结果)

  • 分类问题:结果是离散的(我们设法预测出一个离散值的结果)

无监督学习:

相对于监督学习(给定输入,输出,作为参考),无监督学习不知道输入/输出是什么,可以将所给数据分簇,事先并没有给出分簇规则,只能将数据进行聚类(聚类算法)。

聚类和分类的区别:

  • 分类:已知数据的类型,即在没有输入的时候,就知道输出的结果(如:肿瘤良性还是恶性)。
  • 聚类:不知道数据的类型,只给数据一些特征,机器根据特征,将数据分开。

对称二叉树

Posted on 2019-07-22 | In C++ , 算法题 |
Words count in article: 4 | Reading time ≈ 1

题目

题解

摆渡车

Posted on 2019-07-22 | In C++ , 算法题 |
Words count in article: 4 | Reading time ≈ 1

题目

题解

1…161718…38
zzm99

zzm99

372 posts
40 categories
3 tags
GitHub
0%
© 2020 zzm99 | Site words total count: 409.1k
Powered by Hexo
|
Theme — NexT.Gemini v5.1.4