登录论坛

查看完整版本 : 如何在MATLAB中创建规则排列的值数组?


poster
2019-12-10, 16:49
如何制作以起点,终点和总数组大小定义的数组?像数组的东西,从1到10,即20个元素长。例如,数组可能类似于:

1 1.5 2 2.5 3 3.5 ...
回答:
有两种方法可以执行此操作:


使用冒号运算符 (https://www.mathworks.com/help/matlab/ref/colon.html) :

startValue = 1; endValue = 10; nElements = 20; stepSize = (endValue-startValue)/(nElements-1); A = startValue:stepSize:endValue;
使用linspace (https://www.mathworks.com/help/matlab/ref/linspace.html)函数(由Amro (https://stackoverflow.com/a/1853950/52738)建议):

startValue = 1; endValue = 10; nElements = 20; A = linspace(startValue,endValue,nElements);
请记住,结果数组中的元素数包括端点。在以上示例中,数组元素值之间的差将为9/19或略小于 0.5 (与问题中的示例数组不同)。



更多&回答... (https://stackoverflow.com/questions/1853891)