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

后端 未结 5 759
旧巷少年郎
旧巷少年郎 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:17

    Matlab/Octave allows this king of really-not-efficient but very-convenient notation, assuming a is a structure only containing column-vectors:

    x = [];             #% A fresh new vector/matrix/tensor, who knows?
    for i=1:numel(a)    #% parse container item by item
       x = [x;a{i}];    #% append container item a{i} to x in a column-fashion way
    end
    

    This will works but it is bloody inefficient since it will reallocate x each for step and it is not bulletproof (no error handling, no type checking): therefore it will fail if it encounters anything (matrix, string, row vector) but column vector which are likely to be found in such containers.

    Anyway, it will ease a not-so-stringent-and-heuristic design, but please consider reimplementing when robust design is needed.

提交回复
热议问题