Convert integer to logical array in MATLAB

后端 未结 3 1517
滥情空心
滥情空心 2021-01-21 04:21

I want to convert an integer i to a logical vector with an i-th non-zero element. That can de done with 1:10 == 2, which returns

0              


        
3条回答
  •  -上瘾入骨i
    2021-01-21 04:29

    You can use bsxfun:

    >> bsxfun(@eq, 1:10, [2 5].')
    ans =
    
       0   1   0   0   0   0   0   0   0   0
       0   0   0   0   1   0   0   0   0   0
    

    Note the transpose .' on the second vector; it's important.

提交回复
热议问题