Creating a linearly spaced array of a particular size

后端 未结 2 1463
生来不讨喜
生来不讨喜 2021-01-28 14:47

I am new to MATLAB and currently working on my homework assignment. I am trying to declare the x variable as the following:

Create a linearl

相关标签:
2条回答
  • 2021-01-28 15:37

    You can use linspace as follow:

    x = linspace(-pi, pi, 200);
    

    check this out for an example: https://www.mathworks.com/help/matlab/ref/linspace.html

    0 讨论(0)
  • 2021-01-28 15:43

    The other answer shows how to use linspace, this is the correct method.

    But you can also use the colon operator and some simple arithmetic to do this:

    1. x = -pi : 2*pi/199 : pi -- This means: go from -π to π in steps of such a size that we get exactly 200 values.

    2. x = (0:199) * (2*pi/199) - pi -- This means: create an array with 200 integer values, then scale them to the right range.

    Note that you shouldn't use square brackets [] here. They are for concatenating arrays. The colon operator creates a single array, there is nothing to concatenate it with.

    0 讨论(0)
提交回复
热议问题