how to split up column vectors into separate vectors from data

조회 수: 21 (최근 30일)
audi23
audi23 2019년 5월 28일
댓글: audi23 2019년 5월 28일
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!

채택된 답변

Jan
Jan 2019년 5월 28일
편집: Jan 2019년 5월 28일
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
Jan
Jan 2019년 5월 28일
@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.
audi23
audi23 2019년 5월 28일
Great, thanks for all that help!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2019년 5월 28일
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by