Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2023.1.0 - English


linspace

generates linearly spaced numbers between 2 reached bounds

Syntax

row = linspace(x1, x2)
row = linspace(x1, x2, n)
Matrix = linspace(Col1, Col2)
Matrix = linspace(Col1, Col2, n)

Arguments

x1, x2

Real or complex scalars, or encoded integer scalars: Bounds between which values must be generated.

Col1, Col2

Column vectors of real or complex numbers, or of encoded integers, of same heights.

n

integer number of requested values or columns. Default value: 100

row

row vector of n numbers.

Matrix

Matrix with n columns of numbers.

Description

linspace(x1, x2) generates a row vector of n equally spaced values ranging exactly from x1 to x2.

The syntax y1:y2 or y1:step:y2 like 1:0.1:%pi does the same but fixes the starting bound y1 and the step. The y2 is used as stopping bound to not be overstepped. The last value actually generated may not reach it. y2 is then not included in the result.

Instead of fixing the step to a given value, linspace fixes the final bound x2 to be exactly reached, and computes the step accordingly.

If x1 or x2 are complex numbers, then linspace(x1,x2) interpolates separately the real and the imaginary parts of x1 and x2.

If some column vectors Col1 and Col2 are provided, linspace works in a row-wise way: the resulting Matrix has the same number of rows, and n columns. We get Matrix(i,:) = linspace(Col1(i), Col2(i), n).

When specified bounds are encoded integers, the actual step may vary by one unit along the generated series.

Examples

linspace(1, %pi, 0)         // n = 0
linspace(1, 2, 10)          // x2 > x1 : increasing values
linspace(2, 1, 10)          // x2 < x1 : decreasing values
linspace(1+%i, 2-2*%i, 5)     // with complex numbers
linspace([1:4]', [5:8]', 10)  // with input columns
--> linspace(1, %pi, 0)    // n = 0
 ans  =
    []

--> linspace(1, 2, 10)    // x2 > x1 : increasing values
 ans  =
   1.   1.111   1.222   1.333   1.444   1.556   1.667   1.778   1.889   2.

--> linspace(2, 1, 10)    // x2 < x1 : decreasing values
 ans  =
   2.   1.889   1.778   1.667   1.556   1.444   1.333   1.222   1.111   1.

--> linspace(1+%i, 2-2*%i, 5)      // with complex numbers
 ans  =
   1. +i     1.25 +0.25i   1.5 -0.5i   1.75 -1.25i   2. -2.i

--> linspace([1:4]', [5:8]', 10)   // with input columns
 ans  =
   1.   1.444   1.889   2.333   2.778   3.222   3.667   4.111   4.556   5.
   2.   2.444   2.889   3.333   3.778   4.222   4.667   5.111   5.556   6.
   3.   3.444   3.889   4.333   4.778   5.222   5.667   6.111   6.556   7.
   4.   4.444   4.889   5.333   5.778   6.222   6.667   7.111   7.556   8.

With encoded integers: The step may vary by one unit along the series:

x = linspace(int8([5;127]), int8([127;5]), 10)
x(:,1:$-1) - x(:,2:$)
--> x = linspace(int8([5;127]), int8([127;5]), 10)
 ans  =
    5   18   32  45  59  72  86  99  113  127
  127  114  100  87  73  60  46  33   19    5

--> x(:,1:$-1) - x(:,2:$)
 ans  =
 -13 -14 -13 -14 -13 -14 -13 -14 -14
  13  14  13  14  13  14  13  14  14
// shape interpolation between a sphere and a cone
[T,P]=meshgrid(linspace(0,2*%pi,32),linspace(-%pi/2,%pi/2,32));
X=cos(T).*cos(P);
Y=sin(T).*cos(P);

Z=linspace(sin(P),cos(P)-1,100);

h=uicontrol("style","slider","units","normalized",...
"position",[0.2 0.03 0.6 0.05],"min",1,"max",100,...
"callback",...
"drawlater;delete(gca().children);mesh(X,Y,Z(:,:,h.value));isoview;drawnow")

execstr(h.callback)

See also

  • colon — Ranging operator. Addresses all elements along an array dimension or of a list.
  • logspace — logarithmically spaced vector
  • grand — Random numbers

History

VersionDescription
5.4.0
  • Column vectors can now be provided.
  • The third input argument (n) must be an integer value.
6.0
  • linspace(a, b, n<=0) now returns [] instead of b.
  • bounds are now checked against %inf or %nan values.
6.0.2 linspace() can now be reliably used for series of encoded integers.
Report an issue
<< eye Matrix generation logspace >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon May 22 12:37:05 CEST 2023