Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2024.1.0 - Français


empty []

empty matrix. Array ranges destructor.

Description

General [ ] properties

Empty brackets [] represent the empty matrix. Its general properties are now described.

  1. It has only two dimensions. Any dimension > #2 is automatically squeezed:

    --> e = ones(1,2,0,2); size(e)
     ans  =
       0.   0.
    
    --> e == []
     ans  =
      T
    

  2. It is always of real decimal type. There is no empty matrix of integer types (int8, uint8, int16, uint16, int32, uint32, int64, uint64), nor of string type, etc:

    --> type(uint8([]))  // not of type 8 (encoded integers)
     ans  =
       1.
    
    --> a = [1 2 ; 3 4] + %i;
    --> a(1,:) = []
     a  =
       3. + i     4. + i
    
    --> a(1,:) = [], isreal(a)
     a  =
        []
     ans  =
      T
    
    --> t = "abcd efg", type(t)
     t  =
     abcd efg
    
     ans  =
       10.
    
    --> t(1) = [], type(t)
     t  =
        []
    
     ans  =
       1.
    

  3. However, it is distinct from the sparse empty matrix:

    --> se = sparse([])
     se  =
    (  0,  0) zero sparse matrix
    
    --> size(se)
     ans  =
       0.   0.
    
    --> se == []
     ans  =
      F
    

  4. It is also distinct from all empty heterogeneous containers list(), struct() or cell() :

    --> L = list()
     L  =
         ()
    --> L == []
     ans  =
      F
    
    --> s = struct()
     s  =
    0x0 struct array with no fields.
    --> s == []
     ans  =
      F
    
    --> c = cell()
     c  =
       {}
    --> c == []
     ans  =
      F
    

[ ] as operand or input argument
  1. As operand of usual predefined non-boolean operators, [] sets the result to []. All the following operations yield []:

    Unary operators
    []', [].', -[], ~[]
    Binary numerical operators
    addition: [] + [1 2], [1 2] + []
    subtraction: [] - [1 2], [1 2] - []
    division: []/[1 2], []./[1 2], [1 2]/[], [1 2]./[]
    left division: []\[1 2], [].\[1 2], [1 2]\[], [1 2].\[]
    multiplication:[]*[1 2], [].*[1 2], [1 2]*[], [1 2].*[]
    kronecker: [].*.[1 2], [1 2].*.[]
    power: []^[1 2], [].^[1 2], [1 2]^[], [1 2].^[]
    Inequality comparisons
    greater:[]>[1 2], []>=[1 2], [1 2]>[], [1 2]>=[]
    less:[]<[1 2], []<=[1 2], [1 2]<[], [1 2]<=[]

  2. As operand of boolean binary operators, [] is equivalent to %T:

    Binary numerical operators
    or: [] | [%T %F], [%T %F] | [][%T %T]
    and:[] & [%T %F], [%T %F] & [][%T %F]

    But, noticeably:
    • or([]) is %F.

    • As the condition of any if or while statement, [] is %F:

      --> if []
      -->     r = "[] is %T as any if condition";
      --> else
      -->     r = "[] is %F as any if condition";
      --> end
      --> r
       r  =
       [] is %F as any if condition
      

  3. In concatenations, [] is simply ignored: [A,[]] == [[],A] == [A ; []] == [[] ; A] == A

  4. In text concatenations, +[] yields []: []+["a" "bc"] == ["a" "bc"]+[] == []

  5. As special input matrix of linear algebra or common functions, the answer depends on the considered function. It is documented in the page dedicated to each function. Examples:

    det([]) 1
    rank([]) 0
    trace([]) 0
    norm([]) 0
    cond([]) 0
    rcond([]) Inf
    diag([]) []
    tril([]) []
    triu([]) []
    min([]) []
    max([]) []
    sign([]) []
    clean([]) []
    svd([]) []
    cumprod([]) []
    cumsum([]) []
    sum([]) 0
    prod([]) 1
    mean([]) Nan
    median([]) Nan
    stdev([]) Nan
    mad([]) Nan
    variance([])Nan

  6. As general input argument of functions, [] is often used to choose the default value of an input argument (to somewhat skip it, to avoid providing an actual explicit value). However, this is not a strict rule.

Assigning [ ] to delete ranges in arrays

Considering an array of any number of dimensions and of any size, that can be a matrix or hypermatrix of any datatype, an array of structures, or an array of cells, [] can be assigned to delete the addressed ranges (rows, columns, etc). These ones must cover the full size of the array at least along one of its dimensions.

Examples:

With a matrix of decimal numbers:

a = grand(3,5,"uin",0,9)
--> a = grand(3,5,"uin",0,9)
 a  =
   2.   4.   8.   0.   9.
   2.   1.   3.   6.   4.
   4.   9.   5.   9.   7.

--> a(:,[3 5]) = []
 a  =
   2.   4.   0.
   2.   1.   6.
   4.   9.   9.

--> a(2,:) = []
 a  =
   2.   4.   0.
   4.   9.   9.

With an hypermatrix of texts:

cs = cumsum(grand(2,4,3,"uin",1,3));
t = matrix(strsplit(ascii(grand(1,cs($),"uin",ascii("a"),ascii("c"))),cs(1:$-1)),2,4,3)
--> cs = cumsum(grand(2,4,3,"uin",1,3));
--> t = matrix(strsplit(ascii(grand(1,cs($),"uin",ascii("a"),ascii("c"))),cs(1:$-1)),2,4,3)
 t  =
(:,:,1)
!ccc  b    b   b  !
!bbb  bcc  bc  c  !

(:,:,2)
!aa  aab  bc  a   !
!ab  a    cc  ba  !

(:,:,3)
!c   aba  c    abb  !
!bc  cc   acb  c    !

--> t(:,3,:) = []  // Deleting all 3rd columns
 t  =
(:,:,1)
!ccc  b    b  !
!bbb  bcc  c  !

(:,:,2)
!aa  aab  a   !
!ab  a    ba  !

(:,:,3)
!c   aba  abb  !
!bc  cc   c    !

--> t(:,:,2) = []   // Deleting the 2nd page
 t  =
(:,:,1)
!ccc  b    b  !
!bbb  bcc  c  !

(:,:,2)
!c   aba  abb  !
!bc  cc   c    !

With an array of cells:

c = cat(3, {"start", -1.23, %f  ; (1-%s)^2, gda(), list(2,,%z)}, ..
           {%t     , "abc", 5.2 ; int8(21), []   , %z})
--> c = cat(3, {"start", -1.23, %f  ; (1-%s)^2, gda(), list(2,,%z)}, ..
               {%t     , "abc", 5.2 ; int8(21), []   , %z})
 c  =
(:,:,1)
  [1x1 string    ]  [1x1 constant]  [1x1 boolean]
  [1x1 polynomial]  [1x1 handle  ]  [    list   ]

(:,:,2)
  [1x1 boolean]  [1x1 string  ]  [1x1 constant  ]
  [1x1 int8   ]  [0x0 constant]  [1x1 polynomial]

--> c(:,2,:) = []                   // Deleting all 2nd columns
 c  =
(:,:,1)
  [1x1 string    ]  [1x1 boolean]
  [1x1 polynomial]  [    list   ]

(:,:,2)
  [1x1 boolean]  [1x1 constant  ]
  [1x1 int8   ]  [1x1 polynomial]

--> c(1,:,:) = []                   // Deleting all 1st rows
 c  =
(:,:,1)
  [1x1 polynomial]  [ list]

(:,:,2)
  [1x1 int8]  [1x1 polynomial]

With an array of structures:

--> s(4,5).r = %pi;
--> s.b = %t
 s  =
4x5 struct array with fields:
   r
   b

--> s([1 3],:) = []
 s  =
2x5 struct array with fields:
   r
   b

--> s(:,2) = []
 s  =
2x4 struct array with fields:
   r
   b

Other examples

type(string([]))
[type(int8([])) , type(int16([])) , type(int32([])) , type(int64([]))]
[type(uint8([])), type(uint16([])), type(uint32([])), type(uint64([]))]
[] * %F
--> type(string([]))
 ans  =
   1.

--> [type(int8([])) , type(int16([])) , type(int32([])) , type(int64([]))]
 ans  =
   1.   1.   1.   1.

--> [type(uint8([])), type(uint16([])), type(uint32([])), type(uint64([]))]
 ans  =
   1.   1.   1.   1.

--> [] * %F
 ans  =
    []

A = [%s-1, %s^2]
A + []
A - []
A * []
--> A = [%s-1, %s^2]
 A  =
           2
  -1 +s   s

--> A + []
 ans  =
    []

--> A - []
 ans  =
    []

--> A * []
 ans  =
    []

string([]) == []
["a" "bc"] + []
[] + ["a" "bc"]
--> string([]) == []
 ans  =
  T

--> ["a" "bc"] + []
 ans  =
    []

--> [] + ["a" "bc"]
 ans  =
    []

A = rand(2,2);
A([],:)
--> A = rand(2,2);
--> A([],:)
 ans  =
    []

[det([]) rank([]) trace([]) norm([]) cond([]) rcond([])]
--> [det([]) rank([]) trace([]) norm([]) cond([]) rcond([])]
 ans  =
   1.   0.   0.   0.   0.   Inf

[sum([]) prod([]) mean([]) median([]) stdev([]) mad([])]
--> [sum([]) prod([]) mean([]) median([]) stdev([]) mad([])]
 ans  =
   0.   1.   Nan   Nan   Nan   Nan

See also

  • null — supprime un élément d'une liste, ou un champ d'une structure, M-liste ou T-liste
  • isempty — check if a variable is an empty matrix or an empty list
  • emptystr — zero length string
  • brackets — Concaténation. Récipients d'une affectation. Résultats d'une function
  • operators — opérateurs de Scilab
  • matrices — Objet Scilab, matrices
  • oldEmptyBehaviour — Contrôle le comportement de l'opération+ et de l'opération- sur la matrice vide
  • insertion — insertion/modification dans une variable

History

VersionDescription
6.0.0
  • A+[], []+A and A-[] now return [] instead of A. []-A now returns [] instead of -A.
  • A>[], A>=[], A<[], A<=[], []>A, []>=A, []<A, and []<=A now return [] instead of an error.
Report an issue
<< diag Matrice - génération eye >>

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 Jun 17 17:52:26 CEST 2024