0

I am trying to understand some Matlab code. One line goes a(1, a(2, :)), where a is a logical matrix. The result of this line is a single logical value. I wonder what this line does?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
fdls2011
  • 37
  • 5

1 Answers1

1

The code selects the entries in the first row of a wherever the second row of a is equal to 1.

In MATLAB, when you index with a number like a(1, [2,3,4]) you get the second, third, and fourth entry in the first row of a. When you use a logical, it pulls out entries that are equal to 1 only so the previous command is equal to a(1, logical([0 1 1 1])) because the second, third, and fourth entry in in the index are equal to 1. Therefore, the command a(1, find(a(2,:))) is equivalent to a(1, a(2, :)).

Steve
  • 3,957
  • 2
  • 26
  • 50