2

There is a function named linspace in matlab and splits interval in given range. For example:

>> x = linspace(-10,5, 10)

x =

  -10.0000   -8.3333   -6.6667   -5.0000   -3.3333   -1.6667         0    1.6667    3.3333    5.0000

How can I find x(4) by doing calculations by hand ?

Benoit_11
  • 13,880
  • 2
  • 23
  • 35
mTuran
  • 2,097
  • 4
  • 31
  • 57

1 Answers1

5

This seems to work -

x = linspace(-10,5, 10)

start = -10;
stop = 5;
num_elements = 10;
index = 4;

out = start + (index-1)*(stop - start)./(num_elements-1)

Output -

x =
  -10.0000   -8.3333   -6.6667   -5.0000   -3.3333   -1.6667    0    1.6667 ...
out =
    -5

Thus, (stop - start)./(num_elements-1) would be the stepsize.

So, if you want the complete array, do this -

complete_array = start : (stop - start)./(num_elements-1) :stop 

But, be careful of the floating point precision issues if you are comparing these results against the linspace results - What is the advantage of linspace over the colon “:” operator?.

Community
  • 1
  • 1
Divakar
  • 212,295
  • 18
  • 231
  • 332