How do I create a regularly-spaced array of values in MATLAB?

后端 未结 3 1744
孤城傲影
孤城傲影 2020-12-17 20:05

How do I make an array that\'s defined with a start point, an end point, and a total array size? Something like an array that goes from 1 to 10 that\'s 20 elements long. For

3条回答
  •  情歌与酒
    2020-12-17 20:43

    There are a couple of ways you can do this:

    • Using the colon operator:

      startValue = 1;
      endValue = 10;
      nElements = 20;
      stepSize = (endValue-startValue)/(nElements-1);
      A = startValue:stepSize:endValue;
      
    • Using the linspace function (as suggested by Amro):

      startValue = 1;
      endValue = 10;
      nElements = 20;
      A = linspace(startValue,endValue,nElements);
      

    Keep in mind that the number of elements in the resulting arrays includes the end points. In the above examples, the difference between array element values will be 9/19, or a little less than 0.5 (unlike the sample array in the question).

提交回复
热议问题