where

Purpose

Returns elements of either a or b depending on condition

Format

c = where(condition, a, b)
Parameters:
  • condition (NxK matrix) – Binary matrix where a 1 indicates an element should be selected from a and a 0 indicates an element should be selected from b.

  • a (Scalar, Nx1 vector, 1xK matrix or NxK matrix) – The elements to return where condition equals 1.

  • b (Scalar, Nx1 vector, 1xK matrix or NxK matrix) – The elements to return where condition equals 0.

Returns:

c (NxK matrix) – The combined data.

Examples

Basic usage

condition = { 1 0 0,
              0 1 0 };

a = { 1 2 3,
      4 5 6 };
b = { 10 20 30,
      40 50 60 };

c = where(condition, a, b);

The code above assigns c to be equal to:

 1       20       30
40        5       60

Vectors and scalars will be expanded to fit the dimensions of the larger inputs.

condition = { 1 0 0,
              0 1 0 };

a = { 1 2 3 };
b = { 10,
      40 };

c = where(condition, a, b);

As we see in the result below, the row vector a has been broadcast as if it was a 2x3 matrix with the same elements in each row, while the column vector b has been broadcast as if it was a 2x3 matrix with the same elements in each column.

 1       10       10
40        2       40

Remarks

  • To return the elements with the greatest (or least) magnitude among two matrices, use maxv() (or minv()).

  • To replace elements according to a binary matrix or vector, use missex().