2

I see people take ==, ~=, >, < between matrixes with a different dimension in parentheses following a matrix to get its entries, like this:

b =

     1     4     7
     2     5     8
     3     6     9

>> b == [1 2 3]

ans =

  3×3 logical array

   1   0   0
   0   0   0
   0   0   0

>> b == [1 4 7]

ans =

  3×3 logical array

   1   1   1
   0   0   0
   0   0   0

>> b == [1 4 5]

ans =

  3×3 logical array

   1   1   0
   0   0   0
   0   0   0

>> b == [1 5 4]

ans =

  3×3 logical array

   1   0   0
   0   1   0
   0   0   0

>> a

a =

     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10

>> a(:, b == [1 4 5])

ans =

     1     4
     1     4
     1     4

>> a(:, b == [1 5 4])

ans =

     1     5
     1     5
     1     5

>> b

b =

     1     4     7
     2     5     8
     3     6     9

>> b > [1 3 2]

ans =

  3×3 logical array

   0   1   1
   1   1   1
   1   1   1

However, I have no idea why these would work. Any explanation about this usage? (My English is not good to describe the question better, hope anyone could edit this question to make it more understandable? Thanks in advance!)

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
user8822312
  • 255
  • 4
  • 14
  • 1
    [Implicit expansion](https://www.mathworks.com/help/matlab/ref/bsxfun.html) – rahnema1 Nov 15 '17 at 03:17
  • more detailed explanation about the implicit, or arithmetic, expantion: https://blogs.mathworks.com/loren/2016/10/24/matlab-arithmetic-expands-in-r2016b/ – Adiel Nov 15 '17 at 06:46
  • A logical and very convenient addition to the M-language since R2016b. IMHO though, it should not have been included. I know a lot has already been discussed on this topic, so let's not redo those discussions here, but the incompatibility with the notations in the rest of linear algebra is indeed quite confusing, as you noted. – Rody Oldenhuis Nov 15 '17 at 08:27

1 Answers1

1

Since R2016b, MATLAB uses implicit expansion. This means that inputs' singleton dimensions (dimensions of size 1) are repeated to be the same size as other inputs.

For example:

b = [1  4  7 
     2  5  8 
     3  6  9]

%% Singleton dimension as rows
b == [x y z]
% is equivalent to
b == repmat([x y z], size(b,1), 1)
% is equivalent to
b == [x  y  z  
      x  y  z 
      x  y  z]

%% Singleton dimension as columns  
b == [x; y; z]
% is equivalent to
b == repmat([x; y; z], 1, size(b,2))
% is equivalent to
b == [x  x  x
      y  y  y
      z  z  z]

All 3 of your examples are the "singleton dimension as rows" case, repeating your comparison row vector down and comparing to each row of b. You can easily see that the outputs are as expected.


Implicit expansion can be used in all versions of MATLAB (including pre-R2016b) with the use of bsxfun. This would look like so:

% Since 2016b
b == [1 2 3];
% All versions (@eq is the '==' equals function)
bsxfun(@eq, b, [1 2 3])
Wolfie
  • 27,562
  • 7
  • 28
  • 55