Got confused with a vector indexed by a matrix, in Matlab

前端 未结 2 736
挽巷
挽巷 2020-12-04 00:19

The following codes runs in Matlab:

a = [1 2 3 4]
b = [ 1 2 3; 1 2 3; 1 2 3]
a(b)

The result of a(b) is a matrix:

[ 1 2 3;         


        
2条回答
  •  误落风尘
    2020-12-04 00:57

    Notice that the return value of a(b) is the same size as b.

    a(b) simply takes each element of b, call it b(i,j), as an index and returns the outputs a(b(i,j)) as a matrix the same size as b. You should play around with other examples to get a more intuitive feel for this:

    b = [4 4 4; 4 4 4];
    a(b) % Will return [4 4 4; 4 4 4]
    c = [5; 5];
    a(c) % Will error as 5 is out of a's index range
    

提交回复
热议问题