MATLAB/OCTAVE Extracting elements from different sized vectors in a cell array

后端 未结 5 761
旧巷少年郎
旧巷少年郎 2021-01-13 21:40

I have a simple question but I can\'t figure it out or find it anywhere. I have a cell array where c{1} is a vector and c{2} is a vector but of different lengths, up to c{i

5条回答
  •  情书的邮戳
    2021-01-13 22:08

    This depends on whether the vectors in c are row or column vectors. But usually the fastest and most compact ways are:

    c={[1 2 3], [4 5 6 7 8], [9 10]}
    cell2mat(c)
    cat(2, c{:})
    

    or

    c={[1 2 3]', [4 5 6 7 8]', [9 10]'}
    % cell2mat(c) % Doesn't work.
    cat(1, c{:})
    

    so personally, I prefer cat.

提交回复
热议问题