Convert integer to logical array in MATLAB

后端 未结 3 1518
滥情空心
滥情空心 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条回答
  •  悲哀的现实
    2021-01-21 04:29

    Another way is to use eye and create a logical matrix that is n x n long, then use the indices to index into the rows of this matrix:

    n = 10;
    ind = [2 5];
    
    E = eye(n,n) == 1;
    out = E(ind, :);
    

    We get:

    >> out
    
    out =
    
         0     1     0     0     0     0     0     0     0     0
         0     0     0     0     1     0     0     0     0     0
    

提交回复
热议问题