我正在尝试插入向量的值,但我似乎无法理解如何正确使用interp1.m。
这就是我所期待的:
clazz.type
答案 0 :(得分:5)
假设您总是希望填充矢量的零值:
library(dplyr)
StrategyDefine <- function(df) {
# define strategy for buy and sell signal here
buySignal <- TRUE
df <- select(df, -Index)
df <- apply(df, 1, FUN = function(x) {
if(x['EMASignal'] == 1 & buySignal == TRUE) {
buySignal <- FALSE
}
if(buySignal == FALSE) {
x['Signal'] <- 1
} else { x['Signal'] <- 0 }
if(buySignal == FALSE & x['EMA_30'] < x['EMA_60']) { # problem arises with this if statement.
x['Signal'] <- 0
buySignal <- TRUE
}
x['buySignal'] <- buySignal
;
x['Signal']
})
return(as.numeric(df))
}
> Data
Index Open High Low Close Volume Adj.Close EMA_2 EMA_30 EMA_60 dailyChange EMASignal
1 2007-08-08 165.50 167.00 163.00 167.00 40800 137.91 136.88738 141.0632 142.2844 0.009063444 0
2 2007-08-09 168.50 169.00 163.00 167.00 45800 137.91 137.56913 140.8598 142.1409 -0.008902077 0
3 2007-08-10 165.00 165.00 161.00 164.00 93400 135.43 136.14304 140.5095 141.9209 -0.006060606 0
4 2007-08-13 166.00 169.00 163.00 163.00 41500 134.60 135.11435 140.1282 141.6809 -0.018072289 0
5 2007-08-14 161.50 165.00 161.50 162.00 24700 133.78 134.22478 139.7187 141.4218 0.003095975 0
6 2007-08-15 161.00 161.00 150.50 151.50 56800 125.11 128.14826 138.7762 140.8870 -0.059006211 0
7 2007-08-16 149.75 150.00 146.00 147.00 68400 121.39 123.64275 137.6545 140.2478 -0.018363940 0
8 2007-08-17 148.50 149.00 143.75 145.75 62800 120.36 121.45425 136.5387 139.5957 -0.018518519 0
9 2007-08-20 146.75 150.00 146.00 146.00 26800 120.57 120.86475 135.5085 138.9719 -0.005110733 0
10 2007-08-21 146.50 146.75 141.25 142.75 43000 117.88 118.87492 134.3712 138.2804 -0.025597270 0
11 2007-08-22 143.50 145.75 143.25 144.00 40800 118.91 118.89831 133.3737 137.6453 0.003484321 0
12 2007-08-23 145.75 152.00 145.50 152.00 52600 125.52 123.31277 132.8670 137.2477 0.042881647 0
13 2007-08-24 149.00 153.00 148.25 152.50 27100 125.93 125.05759 132.4194 136.8767 0.023489933 0
14 2007-08-27 154.50 157.50 150.00 151.00 37300 124.69 124.81253 131.9208 136.4771 -0.022653722 0
15 2007-08-28 152.50 152.50 148.50 148.75 24400 122.84 123.49751 131.3349 136.0300 -0.024590164 0
16 2007-08-29 148.25 154.50 148.00 153.00 59200 126.35 125.39917 131.0133 135.7126 0.032040472 0
17 2007-08-30 153.00 155.00 151.50 153.00 80600 126.35 126.03306 130.7124 135.4056 0.000000000 0
18 2007-08-31 155.50 155.50 151.50 153.00 41000 126.35 126.24435 130.4310 135.1087 -0.016077170 0
19 2007-09-03 153.00 156.00 151.50 153.50 49500 126.76 126.58812 130.1942 134.8350 0.003267974 0
20 2007-09-04 154.00 154.50 153.50 154.00 27900 127.17 126.97604 129.9990 134.5837 0.000000000 0
21 2007-09-05 152.00 154.50 152.00 153.00 37300 126.35 126.55868 129.7636 134.3137 0.006578947 0
22 2007-09-06 153.50 153.50 151.00 152.50 46500 125.93 126.13956 129.5163 134.0389 -0.006514658 0
23 2007-09-07 151.50 153.00 145.00 147.50 41500 121.80 123.24652 129.0185 133.6376 -0.026402640 0
24 2007-09-10 147.50 151.50 142.50 145.25 33500 119.95 121.04884 128.4334 133.1888 -0.015254237 0
25 2007-09-11 146.50 149.75 145.75 145.75 25400 120.36 120.58961 127.9125 132.7682 -0.005119454 0
26 2007-09-12 148.50 148.50 145.50 148.00 151500 122.22 121.67654 127.5453 132.4223 -0.003367003 0
27 2007-09-13 148.00 150.00 147.00 148.25 34200 122.42 122.17218 127.2146 132.0944 0.001689189 0
28 2007-09-14 149.25 150.00 145.00 145.75 23400 120.36 120.96406 126.7724 131.7097 -0.023450586 0
29 2007-09-17 147.00 147.00 143.50 143.50 42000 118.50 119.32135 126.2387 131.2766 -0.023809524 0
30 2007-09-18 145.50 148.00 143.75 145.00 10700 119.74 119.60045 125.8194 130.8983 -0.003436426 0
31 2007-09-19 148.00 148.50 145.50 147.00 48100 121.39 120.79348 125.5336 130.5866 -0.006756757 0
32 2007-09-20 147.50 148.25 144.00 148.00 38900 122.22 121.74449 125.3199 130.3123 0.003389831 0
33 2007-09-21 148.50 148.50 144.25 146.75 56800 121.18 121.36816 125.0528 130.0128 -0.011784512 0
34 2007-09-24 147.00 147.00 144.75 145.00 20800 119.74 120.28272 124.7100 129.6760 -0.013605442 0
35 2007-09-25 147.50 147.50 144.75 145.00 30500 119.74 119.92091 124.3894 129.3502 -0.016949153 0
36 2007-09-26 145.25 148.75 145.00 148.75 28900 122.84 121.86697 124.2894 129.1368 0.024096386 0
37 2007-09-27 149.00 150.50 147.25 149.25 43400 123.25 122.78899 124.2223 128.9438 0.001677852 0
38 2007-09-28 152.00 152.00 148.50 151.00 42600 124.69 124.05633 124.2525 128.8043 -0.006578947 0
39 2007-10-01 151.00 151.50 149.50 150.50 32200 124.28 124.20544 124.2543 128.6560 -0.003311258 0
40 2007-10-02 152.00 152.00 146.25 146.75 88700 121.18 122.18848 124.0560 128.4109 -0.034539474 0
41 2007-10-03 149.50 149.50 144.00 144.50 54400 119.33 120.28283 123.7511 128.1131 -0.033444816 0
42 2007-10-04 144.75 145.00 142.50 144.25 23200 119.12 119.50761 123.4523 127.8183 -0.003454231 0
43 2007-10-05 145.75 146.00 144.25 145.50 10400 120.15 119.93587 123.2392 127.5669 -0.001715266 0
44 2007-10-08 146.50 147.00 145.50 147.00 6000 121.39 120.90529 123.1199 127.3643 0.003412969 0
45 2007-10-09 145.50 149.75 145.50 146.25 143900 120.77 120.81510 122.9683 127.1481 0.005154639 0
46 2007-10-10 147.00 149.00 147.00 147.25 28800 121.60 121.33837 122.8800 126.9662 0.001700680 0
47 2007-10-11 147.50 149.25 145.50 148.00 41100 122.22 121.92612 122.8375 126.8106 0.003389831 0
48 2007-10-12 148.00 149.75 148.00 149.75 149800 123.66 123.08204 122.8905 126.7073 0.011824324 0
49 2007-10-15 150.00 150.50 148.50 149.25 35200 123.25 123.19401 122.9137 126.5940 -0.005000000 0
50 2007-10-16 151.00 151.00 146.00 147.50 32000 121.80 122.26467 122.8419 126.4368 -0.023178808 0
51 2007-10-17 147.50 148.50 145.75 148.50 21200 122.63 122.50822 122.8282 126.3120 0.006779661 0
52 2007-10-18 149.00 149.25 146.50 148.50 36100 122.63 122.58941 122.8154 126.1912 -0.003355705 0
53 2007-10-19 149.75 149.75 148.25 149.00 64300 123.04 122.88980 122.8299 126.0879 -0.005008347 0
54 2007-10-22 148.00 149.00 146.00 147.50 101300 121.80 122.16327 122.7634 125.9473 -0.003378378 0
55 2007-10-23 147.00 149.50 147.00 149.00 139200 123.04 122.74776 122.7813 125.8520 0.013605442 0
56 2007-10-24 147.50 148.75 145.00 147.00 36400 121.39 121.84259 122.6915 125.7057 -0.003389831 0
57 2007-10-25 147.25 148.75 147.00 148.00 19400 122.22 122.09420 122.6611 125.5914 0.005093379 0
58 2007-10-26 146.50 147.25 145.50 147.25 17800 121.60 121.76473 122.5927 125.4606 0.005119454 0
59 2007-10-29 148.00 148.00 146.00 146.75 30300 121.18 121.37491 122.5015 125.3202 -0.008445946 0
60 2007-10-30 147.25 148.00 145.75 146.75 282500 121.18 121.24497 122.4163 125.1845 -0.003395586 0
61 2007-10-31 147.75 149.50 147.50 149.00 25300 123.04 122.44166 122.4565 125.1142 0.008460237 0
62 2007-11-01 149.50 152.00 146.00 147.00 251100 121.39 121.74055 122.3877 124.9921 -0.016722408 0
63 2007-11-02 148.75 148.75 145.75 146.75 28400 121.18 121.36685 122.3098 124.8671 -0.013445378 0
64 2007-11-05 145.50 146.75 140.50 141.75 163400 117.06 118.49562 121.9711 124.6111 -0.025773196 0
65 2007-11-06 143.50 144.00 141.50 142.00 21300 117.26 117.67187 121.6671 124.3701 -0.010452962 0
66 2007-11-07 142.50 143.50 138.25 143.25 174500 118.29 118.08396 121.4493 124.1707 0.005263158 0
67 2007-11-08 138.50 145.25 138.50 144.25 92500 119.12 118.77465 121.2990 124.0051 0.041516245 0
68 2007-11-09 148.50 148.50 145.00 145.25 121700 119.95 119.55822 121.2120 123.8722 -0.021885522 0
69 2007-11-12 145.25 146.00 143.50 145.50 53800 120.15 119.95274 121.1434 123.7501 0.001721170 0
70 2007-11-13 144.50 149.00 144.50 148.75 146900 122.84 121.87758 121.2529 123.7203 0.029411765 0
71 2007-11-14 150.00 154.50 148.50 151.50 129600 125.11 124.03253 121.5017 123.7659 0.010000000 0
72 2007-11-15 150.50 152.00 147.25 149.00 31100 123.04 123.37084 121.6010 123.7421 -0.009966777 0
73 2007-11-16 149.00 150.00 146.25 149.75 117000 123.66 123.56361 121.7338 123.7394 0.005033557 0
74 2007-11-19 149.00 150.50 148.00 149.50 70700 123.46 123.49454 121.8452 123.7302 0.003355705 0
75 2007-11-20 148.25 151.50 148.25 150.50 71400 124.28 124.01818 122.0023 123.7482 0.015177066 0
76 2007-11-21 151.00 154.00 149.00 150.50 386000 124.28 124.19273 122.1492 123.7657 -0.003311258 0
77 2007-11-22 150.50 150.50 144.75 144.75 139800 119.53 121.08424 121.9802 123.6268 -0.038205980 0
78 2007-11-23 146.25 146.25 142.75 144.75 126400 119.53 120.04808 121.8222 123.4925 -0.010256410 0
79 2007-11-26 144.50 147.50 142.75 144.00 48600 118.91 119.28936 121.6343 123.3422 -0.003460208 0
80 2007-11-27 142.00 145.50 142.00 143.50 37000 118.50 118.76312 121.4321 123.1835 0.010563380 0
81 2007-11-28 145.00 146.00 142.00 146.00 38600 120.57 119.96771 121.3765 123.0978 0.006896552 0
82 2007-11-29 146.75 153.50 146.50 149.25 268500 123.25 122.15590 121.4973 123.1028 0.017035775 0
83 2007-11-30 153.00 153.00 149.00 152.00 71800 125.52 124.39863 121.7569 123.1820 -0.006535948 0
84 2007-12-03 148.25 150.00 148.25 150.00 38600 123.87 124.04621 121.8932 123.2046 0.011804384 0
85 2007-12-04 148.00 148.50 139.50 142.75 205600 117.88 119.93540 121.6343 123.0300 -0.035472973 0
86 2007-12-05 143.75 146.00 141.75 143.75 111800 118.71 119.11847 121.4456 122.8884 0.000000000 0
87 2007-12-06 144.50 145.25 142.50 145.25 42800 119.95 119.67282 121.3491 122.7920 0.005190311 0
88 2007-12-07 144.00 147.00 144.00 145.50 121900 120.15 119.99094 121.2718 122.7054 0.010416667 0
89 2007-12-10 144.00 149.50 144.00 148.75 88000 122.84 121.89031 121.3729 122.7098 0.032986111 0
90 2007-12-11 148.50 150.50 148.00 150.50 51800 124.28 123.48344 121.5605 122.7613 0.013468013 0
91 2007-12-12 148.25 149.00 147.50 148.00 21500 122.22 122.64115 121.6030 122.7436 -0.001686341 0
92 2007-12-13 148.00 149.00 145.00 147.50 39300 121.80 122.08038 121.6157 122.7126 -0.003378378 0
93 2007-12-14 146.75 147.50 144.50 144.50 40400 119.33 120.24679 121.4683 122.6017 -0.015332198 0
94 2007-12-17 144.00 144.00 139.00 139.00 57000 114.78 116.60226 121.0368 122.3453 -0.034722222 0
95 2007-12-18 139.00 140.00 130.50 130.75 179600 107.97 110.84742 120.1938 121.8739 -0.059352518 0
96 2007-12-19 130.50 131.00 128.00 129.25 231000 106.73 108.10247 119.3251 121.3774 -0.009578544 0
97 2007-12-20 129.25 129.50 127.00 127.25 210800 105.08 106.08749 118.4061 120.8431 -0.015473888 0
98 2007-12-21 127.25 128.50 126.50 126.75 112700 104.67 105.14250 117.5199 120.3128 -0.003929273 0
99 2007-12-27 125.00 125.00 120.75 122.75 172300 101.37 102.62750 116.4780 119.6917 -0.018000000 0
100 2007-12-28 122.50 122.50 117.00 117.00 396000 96.62 98.62250 115.1968 118.9353 -0.044897959 0
101 2008-01-02 120.25 126.25 119.50 124.50 262600 102.81 101.41417 114.3977 118.4066 0.035343035 0
102 2008-01-03 124.50 125.50 122.75 124.50 81300 102.81 102.34472 113.6501 117.8952 0.000000000 0
103 2008-01-04 125.00 125.50 120.00 120.50 63400 99.51 100.45491 112.7378 117.2924 -0.036000000 0
104 2008-01-07 121.00 123.50 117.00 123.25 52100 101.78 101.33830 112.0308 116.7838 0.018595041 0
105 2008-01-08 123.75 125.50 123.25 124.25 53100 102.60 102.17943 111.4224 116.3188 0.004040404 0
106 2008-01-09 123.75 123.75 119.25 121.00 42100 99.92 100.67314 110.6803 115.7811 -0.022222222 0
107 2008-01-10 121.00 121.25 119.25 120.75 44500 99.71 100.03105 109.9725 115.2542 -0.002066116 0
108 2008-01-11 120.00 123.25 120.00 123.25 49200 101.78 101.19702 109.4440 114.8124 0.027083333 0
109 2008-01-14 121.50 125.50 121.50 125.50 48900 103.64 102.82567 109.0695 114.4461 0.032921811 0
110 2008-01-15 125.75 125.75 122.00 122.00 58500 100.75 101.44189 108.5328 113.9971 -0.029821074 0
111 2008-01-16 120.25 122.75 117.00 120.75 74800 99.71 100.28730 107.9636 113.5286 0.004158004 0
112 2008-01-17 121.50 125.00 121.00 121.00 65400 99.92 100.04243 107.4446 113.0824 -0.004115226 0
113 2008-01-18 121.50 121.50 117.75 118.00 55400 97.44 98.30748 106.7992 112.5696 -0.028806584 0
114 2008-01-21 118.75 119.50 114.50 116.00 95200 95.79 96.62916 106.0889 112.0194 -0.023157895 0
115 2008-01-22 114.50 121.25 112.50 120.00 158900 99.09 98.26972 105.6374 111.5955 0.048034934 0
116 2008-01-23 120.50 121.75 116.25 119.25 142300 98.48 98.40991 105.1756 111.1655 -0.010373444 0
117 2008-01-24 122.00 124.25 118.50 120.75 86800 99.71 99.27664 104.8230 110.7899 -0.010245902 0
118 2008-01-25 122.00 122.00 118.50 120.50 76000 99.51 99.43221 104.4802 110.4201 -0.012295082 0
119 2008-01-28 120.50 121.00 118.50 119.25 16500 98.48 98.79740 104.0931 110.0286 -0.010373444 0
120 2008-01-29 124.00 124.00 121.50 122.50 29100 101.16 100.37247 103.9039 109.7378 -0.012096774 0
121 2008-01-30 123.25 123.25 119.75 120.25 37300 99.30 99.65749 103.6068 109.3956 -0.024340771 0
122 2008-01-31 122.00 122.00 118.00 120.25 25800 99.30 99.41916 103.3290 109.0646 -0.014344262 0
123 2008-02-01 120.00 122.25 120.00 122.25 35800 100.95 100.43972 103.1755 108.7985 0.018750000 0
124 2008-02-04 123.25 125.50 122.75 124.25 36700 102.60 101.87991 103.1384 108.5953 0.008113590 0
125 2008-02-05 124.25 124.25 120.25 121.50 32700 100.33 100.84664 102.9572 108.3243 -0.022132797 0
126 2008-02-06 120.00 121.75 116.00 118.00 46100 97.44 98.57555 102.6012 107.9675 -0.016666667 0
127 2008-02-07 118.50 123.25 115.50 120.25 81600 99.30 99.05852 102.3883 107.6833 0.014767932 0
128 2008-02-08 122.00 122.00 119.75 121.75 40200 100.54 100.04617 102.2690 107.4491 -0.002049180 0
129 2008-02-11 120.00 122.50 119.00 122.50 43900 101.16 100.78872 102.1975 107.2429 0.020833333 0
130 2008-02-12 123.00 123.00 120.50 120.75 29200 99.71 100.06957 102.0370 106.9959 -0.018292683 0
131 2008-02-13 120.50 123.25 119.50 123.25 49400 101.78 101.20986 102.0204 106.8249 0.022821577 0
132 2008-02-14 122.50 123.25 122.00 122.75 25000 101.37 101.31662 101.9784 106.6460 0.002040816 0
133 2008-02-15 122.75 122.75 120.50 122.00 27100 100.75 100.93887 101.8992 106.4527 -0.006109980 0
134 2008-02-18 122.75 122.75 121.00 121.25 26400 100.13 100.39962 101.7850 106.2454 -0.012219959 0
135 2008-02-19 122.25 122.25 121.50 121.75 24300 100.54 100.49321 101.7047 106.0584 -0.004089980 0
136 2008-02-20 121.75 124.25 120.00 122.50 115800 101.16 100.93774 101.6696 105.8978 0.006160164 0
137 2008-02-21 126.00 143.00 126.00 140.00 316400 115.61 110.71925 102.5690 106.2162 0.111111111 0
138 2008-02-22 139.00 145.50 138.75 144.25 50300 119.12 116.31975 103.6368 106.6393 0.037769784 0
139 2008-02-25 146.00 149.25 146.00 148.50 88900 122.63 120.52658 104.8621 107.1635 0.017123288 0
140 2008-02-26 150.00 156.00 150.00 154.00 134700 127.17 124.95553 106.3014 107.8195 0.026666667 0
141 2008-02-27 155.50 155.50 146.50 151.00 33300 124.69 124.77851 107.4877 108.3726 -0.028938907 0
142 2008-02-28 152.00 152.50 149.00 151.50 18200 125.11 124.99950 108.6246 108.9214 -0.003289474 0
143 2008-02-29 151.50 155.50 146.00 154.00 148300 127.17 126.44650 109.8211 109.5197 0.016501650 0
144 2008-03-03 152.50 155.50 151.00 154.50 43000 127.58 127.20217 110.9669 110.1119 0.013114754 0
145 2008-03-04 156.00 161.50 154.00 157.00 163400 129.65 128.83406 112.1722 110.7524 0.006410256 0
146 2008-03-05 157.00 158.00 154.50 156.00 90000 128.82 128.82469 113.2463 111.3448 -0.006369427 0
147 2008-03-06 152.00 157.50 152.00 154.50 53200 127.58 127.99490 114.1710 111.8771 0.016447368 0
148 2008-03-07 152.00 155.00 145.00 153.00 260900 126.35 126.89830 114.9568 112.3516 0.006578947 0
149 2008-03-10 151.50 153.00 145.00 149.00 43200 123.04 124.32610 115.4783 112.7021 -0.016501650 0
150 2008-03-11 147.00 152.00 141.00 150.00 35400 123.87 124.02203 116.0197 113.0682 0.020408163 0
151 2008-03-12 151.00 151.00 148.50 148.50 39500 122.63 123.09401 116.4461 113.3817 -0.016556291 0
152 2008-03-13 148.00 150.00 145.25 148.75 183500 122.84 122.92467 116.8586 113.6919 0.005067568 0
153 2008-03-14 149.75 151.00 147.00 150.00 45000 123.87 123.55489 117.3110 114.0256 0.001669449 0
154 2008-03-17 146.75 153.00 146.75 152.00 44100 125.52 124.86496 117.8406 114.4024 0.035775128 0
155 2008-03-18 153.00 155.50 150.00 154.00 20600 127.17 126.40165 118.4425 114.8210 0.006535948 0
156 2008-03-19 152.00 161.00 152.00 159.50 63500 131.71 129.94055 119.2985 115.3748 0.049342105 0
157 2008-03-20 159.50 159.50 155.00 155.00 20200 128.00 128.64685 119.8599 115.7887 -0.028213166 0
158 2008-03-25 157.00 163.50 154.00 160.00 36100 132.13 130.96895 120.6515 116.3245 0.019108280 0
159 2008-03-26 158.00 160.00 154.00 155.50 22000 128.41 129.26298 121.1520 116.7207 -0.015822785 0
160 2008-03-27 156.50 169.00 152.50 163.00 65700 134.60 132.82099 122.0196 117.3069 0.041533546 0
161 2008-03-28 163.00 168.00 163.00 165.50 59400 136.67 135.38700 122.9648 117.9418 0.015337423 0
162 2008-03-31 166.00 166.00 161.50 164.00 66100 135.43 135.41567 123.7690 118.5152 -0.012048193 0
163 2008-04-01 163.00 172.50 163.00 171.50 71900 141.62 139.55189 124.9207 119.2727 0.052147239 0
164 2008-04-02 171.00 177.00 170.50 174.00 98800 143.69 142.31063 126.1316 120.0733 0.017543860 0
165 2008-04-03 173.50 178.00 171.00 173.00 100400 142.86 142.67688 127.2109 120.8204 -0.002881844 0
166 2008-04-04 171.00 171.00 161.50 163.00 397500 134.60 137.29229 127.6876 121.2722 -0.046783626 0
167 2008-04-07 173.50 178.00 157.50 173.00 133600 142.86 141.00410 128.6665 121.9800 -0.002881844 0
168 2008-04-08 158.50 159.50 152.50 155.00 52400 128.00 132.33470 128.6235 122.1774 -0.022082019 0
169 2008-04-09 152.00 157.50 151.50 157.00 297400 129.65 130.54490 128.6897 122.4224 0.032894737 0
170 2008-04-10 156.00 158.50 155.50 157.00 119500 129.65 129.94830 128.7516 122.6593 0.006410256 0
171 2008-04-11 157.50 159.00 154.50 156.50 45700 129.24 129.47610 128.7832 122.8751 -0.006349206 0
172 2008-04-14 154.50 157.00 152.00 155.50 47600 128.41 128.76537 128.7591 123.0566 0.006472492 0
173 2008-04-15 157.00 158.50 155.00 157.50 12700 130.06 129.62846 128.8430 123.2862 0.003184713 0
174 2008-04-16 159.00 159.00 154.50 156.50 14000 129.24 129.36949 128.8686 123.4814 -0.015723270 0
175 2008-04-17 158.50 159.00 156.50 158.00 47100 130.47 130.10316 128.9719 123.7105 -0.003154574 0
176 2008-04-18 157.00 159.00 157.00 158.50 15800 130.89 130.62772 129.0957 123.9459 0.009554140 0
177 2008-04-21 159.00 159.00 157.50 158.50 37200 130.89 130.80257 129.2114 124.1736 -0.003144654 0
178 2008-04-22 159.00 160.00 153.50 156.50 136900 129.24 129.76086 129.2133 124.3397 -0.015723270 0
179 2008-04-23 157.00 160.00 153.50 158.00 31100 130.47 130.23362 129.2944 124.5407 0.006369427 0
180 2008-04-24 158.00 162.00 158.00 161.50 121000 133.36 132.31787 129.5567 124.8299 0.022151899 0
181 2008-04-25 161.50 170.00 160.50 169.50 54200 139.97 137.41929 130.2285 125.3263 0.049535604 0
182 2008-04-28 168.50 170.00 165.50 167.50 31100 138.32 138.01976 130.7505 125.7523 -0.005934718 1
183 2008-04-29 170.00 171.50 167.00 171.00 78700 141.21 140.14659 131.4253 126.2591 0.005882353 1
184 2008-04-30 170.00 174.00 168.50 170.50 23000 140.80 140.58220 132.0301 126.7358 0.002941176 1
185 2008-05-02 171.50 177.00 171.00 176.50 39100 145.75 144.02740 132.9153 127.3593 0.029154519 1
186 2008-05-05 173.50 177.50 171.00 172.00 7000 142.04 142.70247 133.5040 127.8406 -0.008645533 1
187 2008-05-06 170.00 173.00 169.00 173.00 27800 142.86 142.80749 134.1076 128.3330 0.017647059 1
188 2008-05-07 171.50 177.00 171.50 176.00 44000 145.34 144.49583 134.8323 128.8906 0.026239067 1
189 2008-05-08 174.50 178.50 173.50 178.50 64600 147.40 146.43194 135.6431 129.4975 0.022922636 1
190 2008-05-09 177.50 179.00 174.50 178.00 22600 146.99 146.80398 136.3752 130.0710 0.002816901 1
191 2008-05-12 175.00 178.00 175.00 176.00 9700 145.34 145.82799 136.9535 130.5716 0.005714286 1
192 2008-05-13 176.00 179.50 176.00 179.50 22400 148.23 147.42933 137.6810 131.1506 0.019886364 1
193 2008-05-14 178.50 181.50 178.00 181.50 31800 149.88 149.06311 138.4681 131.7647 0.016806723 1
194 2008-05-15 182.00 183.50 181.00 183.50 49400 151.53 150.70770 139.3108 132.4127 0.008241758 1
195 2008-05-16 184.00 184.50 183.00 183.50 29300 151.53 151.25590 140.0991 133.0395 -0.002717391 1
196 2008-05-19 183.00 183.00 179.00 180.00 106000 148.64 149.51197 140.6501 133.5510 -0.016393443 1
197 2008-05-20 177.00 177.00 175.00 176.00 30600 145.34 146.73066 140.9527 133.9375 -0.005649718 1
198 2008-05-21 176.00 182.00 173.00 173.50 110700 143.27 144.42355 141.1022 134.2435 -0.014204545 1
199 2008-05-22 173.50 173.50 168.00 168.00 111400 142.01 142.81452 141.1608 134.4982 -0.031700288 1
200 2008-05-23 170.00 170.00 164.00 165.50 63600 139.89 140.86484 141.0788 134.6749 -0.026470588
0
a = [3 0 6 0 5 0 4]
mask = logical(a);
nvec = 1:numel(a);
a(~mask) = interp1(nvec(mask),a(mask),nvec(~mask))
假设你想按某个因素拉伸你的矢量:
a =
3.0000 4.5000 6.0000 5.5000 5.0000 4.5000 4.0000
a = [3 6 5 4]
stretchfactor = 2;
a = interp1((1:numel(a))*stretchfactor - 1, a, 1:numel(a)*stretchfactor - 1)
答案 1 :(得分:3)
我认为您打算使用interp1
:
a=[1 2 3 4];
N=7; % # of points to interpolate between a(1)=1 and a(end)=4
xi=linspace(a(1),a(end),N); % the new intep x-grid
ai=interp1(1:numel(a),a,xi)
作为旁注,如果您只想在任意两个值之间使用线性间距,请使用linspace
,例如:
linspace(1,4,10)
ans =
1.0000 1.3333 1.6667 2.0000 2.3333 2.6667 3.0000 3.3333 3.6667 4.0000
零是您输入的实际值,如果您对它们进行插值,则“强制”将结果传递给它们...