how to split up column vectors into separate vectors from data
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
audi23
el 28 de Mayo de 2019
Comentada: audi23
el 28 de Mayo de 2019
I have a large amount of data. I'm using import data function. The data is tracking information, the first column is the time period starting at 1....n , but n is usually different. Each time it repeats i.e time starts at 1 again, its tracking a different thing, so I want to split up the data into seperate vectors so I can look at them seperatly, rather than just have one long column vector. The data has 4 columns, including the time period, so I want to group them together for each new track. Or is there a simpler way to analyze each track?
Thanks in advance!
0 comentarios
Respuesta aceptada
Jan
el 28 de Mayo de 2019
Editada: Jan
el 28 de Mayo de 2019
With a simple loop:
Data = cat(2, [1:4, 1:6].', rand(10, 3)); % Some test data
ind1 = [find(Data(:, 1) == 1); size(Data, 1) + 1];
% [EDITED] Typo fixed: ^ this was a comma
nPart = numel(ind1) - 1;
Part = cell(1, nPart);
for iPart = 1:nPart
Part{iPart} = Data(ind1(iPart):ind1(iPart + 1)-1, :);
end
Now all parts are separated at the indices, which contain a 1 in the first column of Data.
3 comentarios
Jan
el 28 de Mayo de 2019
@audi23: This was a typo. You could try to fix some problems by your own. You see this line:
ind1 = [find(Data(:, 1) == 1), size(Data, 1) + 1];
The message tells you, that horzcat gets inputs with not matching dimensions. horzcat is the function, which is abbreciated to "[ , ]". Now you can let Matlab stop at the error:
dbstop if error
Run the code again until it stops in this line. Then check the sizes of the arguments:
size(find(Data(:, 1) == 1))
size(size(Data, 1) + 1)
You will see, that a horizontal concatenation cannot work, because one is a [n x 1] vector and the other a scalar.
The solution is easy: Use a vertical concatenation "[ ; ]" with a semicolon instead of a comma. See [EDITED] in my answer.
Más respuestas (1)
dpb
el 28 de Mayo de 2019
Build a ThingNum tracking id variable for each and then use findgroups and splitapply to analyze by group however desired.
The ID variable is relatively easy to build given you can find all locations with Time==1 and then each sequence from first element to the subsequent less one is the given event.
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!