高亮颜色说明:突出重点
个人觉得,:待核准个人观点是否有误
高亮颜色超链接
文章目录
- 生成序列
- 生成序列 by Matlab
- 生成序列 by Python
- 生成等差数列
- 生成等差数列 by Matlab
- 生成等差数列 by Numpy
- 一级标题
- 二级标题
- 待补充
本篇博文内容概要for查找:
生成序列
生成序列 by Matlab
在MATLAB中,可以使用多种方法来生成序列。以下是一些常见的方法:
- 使用冒号操作符(:):
sequence = 1:10; % 生成从1到10的序列sequence = 1:2:20; % 生成一个从 1 到 20 的奇数序列
- 使用linspace函数生成线性间隔的序列:
sequence = linspace(1, 10, 10); % 生成从1到10的序列,包含10个均匀分布的点
- 使用logspace函数生成对数间隔的序列:
sequence = logspace(0, 1, 10); % 生成从10^0到10^1的序列,包含10个均匀分布的点
- 使用zeros,ones或rand等函数生成特定大小的序列:
sequence = zeros(1, 10); % 生成1x10的零序列
sequence = ones(1, 10); % 生成1x10的单位序列
sequence = rand(1, 10); % 生成1x10的随机序列
- 使用循环或for循环生成序列:
sequence = [];
for i = 1:10sequence = [sequence, i];
end
生成序列 by Python
为了生成一个序列,Python 提供了多种内置函数和方法。以下是一些常见的方法:
- 使用 range() 函数生成整数序列:
# 生成一个从 1 到 10 的整数序列
sequence = list(range(1, 11))
print(sequence) # 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]# 生成一个从 1 到 20 的奇数序列
sequence = list(range(1, 20, 2))
print(sequence) # 输出: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
- 使用 list comprehension 生成序列:
# 生成一个从 1 到 10 的平方的序列
sequence = [x**2 for x in range(1, 11)]
print(sequence) # 输出: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
- 使用 itertools 模块生成序列:
import itertools# 生成一个从 1 到 20 的奇数序列
seq_iterator = itertools.count(1,2) # itertools.count(start=1, step=2)
sequence = [next(seq_iterator) for _ in range(10)]
print(sequence) # 输出: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]# 生成一个从 1 到 10 的平方的序列
seq_iterator = map(lambda x:x**2,itertools.count(1)) # itertools.count() 搭配 map() 使用
sequence = [next(seq_iterator) for _ in range(10)]
print(sequence) # 输出: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]# 为字符串中的字符编号
seq_tuple = zip(itertools.count(1), 'abcdefg') # itertools.count() 搭配 zip() 使用
print(list(seq_tuple)) # 输出: [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g')]
- 使用 numpy.arange() 函数生成序列:
import numpy as np# 生成一个从 1 到 20 的(整型)奇数序列
sequence = np.arange(1, 20, 2)
print(sequence) # 输出: [ 1 3 5 7 9 11 13 15 17 19]# 生成一个0.5~0.95且间隔为0.05的序列
sequence = np.arange(50,95+5,5) / 100
print(sequence) # 输出: [0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95]
- 使用 numpy.linspace() 函数生成序列:
import numpy as np# 生成一个从 1 到 10 的(浮点型)奇数序列
sequence = np.linspace(1, 19, 10) # numpy.linspace(start, stop, num), num:Number of samples to generate.
print(sequence) # 输出: [ 1. 3. 5. 7. 9. 11. 13. 15. 17. 19.]# 生成一个0.5~0.95且间隔为0.05的序列
sequence = np.linspace(50, 95,int(np.round((0.95 - .5) / .05)) + 1, endpoint=True) / 100
print(sequence) # 输出: [0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95]
生成等差数列
生成等差数列 by Matlab
建议使用 matlab_b
或 matlab_d
来生成序列 [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]。
- 使用冒号操作符(:)生成等差数列:
K>> matlab_a = [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]K>> matlab_b = (50:5:95) / 100matlab_b =0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500K>> matlab_a - matlab_bans =0 0 0 0 0 0 0 0 0 0% 奇怪的是 matlab_c 中得到的matlab_c(7)并不等于0.8,matlab_c(9)并不等于0.9
K>> matlab_c = .5:.05:.95matlab_c =0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500K>> matlab_a - matlab_cans =1.0e-15 *0 0 0 0 0 0 0.1110 0 0.1110 0K>> fprintf('matlab_c(7)=%.16f,\tmatlab_c(9)=%.16f\n',matlab_c(7),matlab_c(9));
matlab_c(7)=0.7999999999999999, matlab_c(9)=0.8999999999999999% "%.16f"表示以保留小数点后16位有效数字输出浮点数;
for i=1:numel(matlab_c)fprintf('matlab_c(i)=%.16f\n',matlab_c(i));
end
- 使用linspace函数生成等差数列:
linspace()
: Generate linearly spaced vector. 生成线性间距向量。
linspace - 生成线性间距向量 - MATLAB
K>> matlab_a = [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]K>> matlab_d = linspace(50,95,round((0.95 - .5) / .05) + 1)/100matlab_d =0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500K>> size(matlab_d)ans =1 10K>> matlab_a - matlab_dans =0 0 0 0 0 0 0 0 0 0% 奇怪的是 matlab_e 中得到的matlab_e(8)并不等于0.85,matlab_e(9)并不等于0.9
K>> matlab_e = linspace(.5,0.95,round((0.95 - .5) / .05) + 1)matlab_e =0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500K>> matlab_a - matlab_eans =1.0e-15 *0 0 0 0 0 0 0 0.1110 0.1110 0K>> fprintf('matlab_e(8)=%.16f,\tmatlab_e(9)=%.16f\n',matlab_e(8),matlab_e(9));
matlab_e(8)=0.8499999999999999, matlab_e(9)=0.8999999999999999
生成等差数列 by Numpy
建议使用 python_b
或 python_d
来生成序列 [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]。
- 使用 numpy.arange() 函数生成等差数列
import numpy as nppython_a = np.array([0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95])python_b = np.arange(50,95+5,5) / 100
Out[11]: array([0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85, 0.9 , 0.95])
# [0:10] : [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]
python_a - python_b
Out[12]:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])# 奇怪的是 python_c 中得到的python_c[2]并不等于0.6,python_c[3]并不等于0.65,......
python_c = np.arange(.5,.95+.05,.05)
Out[13]: array([0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85, 0.9 , 0.95])
# [0:10] : [0.5, 0.55, 0.6000000000000001, 0.6500000000000001, 0.7000000000000002, 0.7500000000000002, 0.8000000000000003, 0.8500000000000003, 0.9000000000000004, 0.9500000000000004]
python_a - python_c
Out[14]:
array([ 0.00000000e+00, 0.00000000e+00, -1.11022302e-16, -1.11022302e-16,-2.22044605e-16, -2.22044605e-16, -2.22044605e-16, -3.33066907e-16,-3.33066907e-16, -4.44089210e-16])print("python_c[2]={:.20f}".format(python_c[2]))
Out[15]: python_c[2]=0.60000000000000008882print("python_a[2]-python_c[2]={:.20f}".format(python_a[2]-python_c[2]))
Out[16]: python_a[2]-python_c[2]=-0.00000000000000011102for i, v in enumerate(python_c):print(f"python_c[%d]=%.20f" % (i,v))# print("python_c[{:d}]={:.20f}".format(i, v))
- 使用 numpy.linspace() 函数生成等差数列
import numpy as nppython_a = np.array([0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95])python_d = np.linspace(50, 95,int(np.round((0.95 - .5) / .05)) + 1, endpoint=True) / 100
Out[21]: array([0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85, 0.9 , 0.95])
# [0:10] : [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]
python_d.shape
Out[22]: (10,)
# [v for v in python_d]
python_a - python_d
Out[23]: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])# 奇怪的是 python_e 中得到的python_e[8]并不等于0.9
python_e = np.linspace(.5, 0.95,int(np.round((0.95 - .5) / .05)) + 1, endpoint=True)
Out[24]: array([0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85, 0.9 , 0.95])
# [0:10] : [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.8999999999999999, 0.95]
python_a - python_e
Out[25]:
array([0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,1.11022302e-16, 0.00000000e+00])np.where(.8 == python_d)[0], np.where(.9 == python_d)[0]
Out[26]: (array([6], dtype=int64), array([8], dtype=int64))
np.where(.8 == python_e)[0], np.where(.9 == python_e)[0]
Out[27]: (array([6], dtype=int64), array([], dtype=int64))print("python_e[8]={:.20f}".format(python_e[8]))
Out[28]: python_e[8]=0.89999999999999991118print("python_a[8]-python_e[8]={:.20f}".format(python_a[8]-python_e[8]))
Out[29]: python_a[8]-python_e[8]=0.00000000000000011102
一级标题
二级标题
待补充