Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - Русский


and

logical AND between the elements of a boolean or numerical array

Syntax

b = and(A)
b = and(A, 'r')
b = and(A, 'c')
b = and(A, n)

Arguments

A

vector, matrix, or hypermatrix of booleans, encoded integers (of any inttype), real, or complex numbers. A may be sparse-encoded.

A number is considered as %F (false) if it is 0 or 0+0i. Otherwise (including %nan), it is equivalent to %T (true).

Special A:
  • and([]) returns %T.
  • and(%nan) returns %T.
b

boolean scalar, if and(A) is used without any option "r", "c", n. Then and(A) sets b to

  • %F, if at least one of A's components is %F or zero.
  • %T, otherwise: All A components are %T or non zero or %nan.

Otherwise: boolean vector, matrix or hypermatrix. See n below.

When A is sparse-encoded, b is so as well.

n

Index <= ndims(A) of the dimension along which and() is applied / projected. By default, and() is applied between all A's elements. Otherwise:

  • n = 1 | "r" : and() is applied row-wise. If A is a matrix, the result b is a row, with b(j) = and(A(:,j))
  • n = 2 | "c" : and() is applied column-wise. If A is a matrix, the result b is a column, with b(i) = and(A(i,:))
  • n > 2 : If A is an hypermatrix with at least n dimensions, and() is applied accross the nth dimension.

    Example: If ndims(A)==3 and n=3, b is a boolean matrix of size size(A)([1 2]), with b(i,j) = and(A(i,j,:))

Description

and() computes a logical AND between the components of the single input A.

To compute AND in an element-wise way between two arrays C and D of same sizes, please use the & operator instead.

Why is and([]) set to %T ? Whatever are compatible B and C, and([B C])==(and(B) & and(C)). Now, for B = [], and([B C])==and(C). To have always (and([]) & and(C))==and(C), and([]) must be %T.

Examples

and([])
and(0)
and(0+0*%i)
and(%eps)
and(%i)
and(%nan)

// Projection accross a dimension / along a direction:
A = rand(2,5)<0.5
and(A)
and(A, "r")   // and(A, 1) does the same
and(A, "c")   // and(A, 2) does the same

// Equivalent application to encoded integers:
A = int16(grand(3,5,"uin",-10,10));
A(abs(A)<3) = 0
and(A)
and(A,1)

// With an hypermatrix of decimal numbers:
A = rand(3,4,2);
A(A<0.2) = 0
and(A,3)

// With a sparse matrix:
A = sprand(70,100, 0.001)
and(A, "r")
and(A, "c")
--> and([])
 ans  =
  T

--> and(0)
 ans  =
  F

--> and(0+0*%i)
 ans  =
  F

--> and(%eps)
 ans  =
  T

--> and(%i)
 ans  =
  T

--> and(%nan)
 ans  =
  T

--> // Projection accross a dimension / along a direction:
--> A = rand(2,5)<0.5
 A  =
  T T F F F
  F T F F T

--> and(A)
 ans  =
  F

--> and(A, "r")   // and(A, 1) does the same
 ans  =
  F T F F F

--> and(A, "c")   // and(A, 2) does the same
 ans  =
  F
  F

--> // Equivalent application to encoded integers:
--> A = int16(grand(3,5,"uin",-10,10));
--> A(abs(A)<3) = 0
 A  =
   0  0  -8  -6    8
 -10  6  -5   3  -10
   0  3 -10   7   10

--> and(A)
 ans  =
  F

--> and(A,1)
 ans  =
  F F T T T

--> // With an hypermatrix of decimal numbers:
--> A = rand(3,4,2);
--> A(A<0.2) = 0
 A  =
(:,:,1)
   0.4052   0.4819   0.2806   0.2119
   0.9185   0.264    0.       0.
   0.       0.4148   0.7783   0.6857
(:,:,2)
   0.       0.4062   0.       0.5896
   0.6971   0.4095   0.       0.6854
   0.8416   0.8784   0.5619   0.8906

--> and(A,3)
 ans  =
  F T F T
  T T F F
  F T T T

--> // With a sparse matrix:
--> A = sprand(70,100, 0.001)
 A  =
(  70,  100) sparse matrix

(  4,  87)    0.6463
(  5,  39)    0.4898
(  7,  92)    0.7094
(  29,  87)   0.794
(  33,   1)   0.4087
(  36,  79)   0.4876
(  54,  65)   0.4456
(  67,  45)   0.458

--> and(A, "r")
 ans  =
(  1,  100)False sparse matrix

--> and(A, "c")
 ans  =
(  70,  1)False sparse matrix

See also

  • & (element-wise) — Binary AND between integers. Logical AND over/between booleans and numbers
  • or — logical OR over the elements of a boolean or numerical array
  • not — (~) логическое НЕ

History

ВерсияОписание
6.0.0 Complex numbers are now accepted.
Report an issue
<< abs Матричные операции cross >>

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:
Thu Oct 24 11:18:31 CEST 2024