Split vector in MATLAB

后端 未结 6 1025
独厮守ぢ
独厮守ぢ 2020-12-15 07:06

I\'m trying to elegantly split a vector. For example,

vec = [1 2 3 4 5 6 7 8 9 10]

According to another vector of 0\'s and 1\'s of the same

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 07:44

    Unfortunately there is no 'inverse concatenate' in MATLAB. If you wish to solve a question like this you can try the below code. It will give you what you looking for in the case where you have two split point to produce three vectors at the end. If you want more splits you will need to modify the code after the loop.

    The results are in n vector form. To make them into cells, use num2cell on the results.

    pos_of_one = 0;
    
    % The loop finds the split points and puts their positions into a vector.
    for kk = 1 : length(cut)
        if cut(1,kk) == 1
            pos_of_one = pos_of_one + 1;
            A(1,one_pos) = kk;
        end
    end
    
    F = vec(1 : A(1,1) - 1);
    G = vec(A(1,1) + 1 : A(1,2) - 1);
    H = vec(A(1,2) + 1 : end);
    

提交回复
热议问题