Convert integer to logical array in MATLAB

后端 未结 3 1516
滥情空心
滥情空心 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:48

    Just another possibility using indexing:

    n = 10;
    ind = [2 5];
    x=zeros(numel(ind),n);
    x(sub2ind([numel(ind),n],1:numel(ind),ind))=1;
    

提交回复
热议问题