How to split up column vector

4 visualizaciones (últimos 30 días)
audi23
audi23 el 1 de Jun. de 2019
Comentada: audi23 el 2 de Jun. de 2019
Say I have a long column vector, I now need to split up the vector into smaller vectors according to sizes given in another vector. For instance, say I want the sizes to be [14;11;51; etc...] I want my main vector to be split up into vectors of sizes 14, 11, 51, etc... and in the same order as my main matrix.
Thanks in advance!

Respuesta aceptada

dpb
dpb el 1 de Jun. de 2019
Editada: dpb el 1 de Jun. de 2019
In general, if you're thinking of starting with vector V and creating subvectors, say, A, B, C, ..., this is a very bad idea. It creates a real mess to be able to refer to those variables later.
To segregate data this way and make reference to it programmatically simple, use something like
ix=[14;11;51]; % define the breakpoint vector
v=1:sum(ix);v=v(:); % create some dummy data of right size
>> mat2cell(v,ix) % create cell array of each group
ans =
3×1 cell array
{14×1 double}
{11×1 double}
{51×1 double}
>>
Or, another approach is to create an auxiliary grouping variable and use findgroups and splitapply or similar techniques to process by group that doesn't require explicitly building the other results as variables.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by