Indexing to return segments

2 次查看(过去 30 天)
Hi. I know this could look like a silly question but can't find a way to do it. I have a matrix (data) that is 14258 x 7, the first six columns are data from a subject and the last column are time markers (0 off and 5 is on).
I used ischange on the last column to have the changing points:
[index,~] = ischange(data(:,7))
The problem is that I should end with 10 segments (of different duration) that include the data of the other 6 columns, but I don't know how to use the index to have it.
Thanks in advance

采纳的回答

Image Analyst
Image Analyst 2021-1-29
This will take the 10 different regions of 5, and 11 different regions of 0 and put them into cell arrays output5 and output0. I need to use cell arrays because not every run has the same number of rows in it.
The code requires the Image Processing Toolbox.
s = load('data.mat')
data = s.data;
% Get the segments with 5 in the last column.
is5 = data(:, end) == 5;
props = regionprops(is5, 'Area', 'PixelList');
allRunLengths = [props.Area] % Just for our information - not used at all.
% Take these 10 runs of lengths 266 352 347 384 383 454 505 536 549 604
% and put them into a cell array
for k = 1 : numel(props)
rows = props(k).PixelList(:, 2);
output5{k} = data(rows, 1:6);
end
% Get the segments with 0 in the last column.
is0 = data(:, end) == 0;
props = regionprops(is0, 'Area', 'PixelList');
allRunLengths = [props.Area] % Just for our information - not used at all.
% Take these 11 runs of lengths 1526 618 611 627 608 1957 601 603 604 613 1510
% and put them into a cell array
for k = 1 : numel(props)
rows = props(k).PixelList(:, 2);
output0{k} = data(rows, 1:6);
end

更多回答(1 个)

Bob Thompson
Bob Thompson 2021-1-29
I can't actually use ischange in my version of MATLAB, so take what I suggest with a grain of salt.
It looks like the index output is actually a logic array. I recommend using find to identify the actual indexes of the identified changes. I imagine it woud look something like the following, but it is untested:
[index] = find(ischange(data(:,7))==1);
I'm not quite sure how you want to utilize the different sections, but you could capture the different time elements in a cell array kind of like the following:
timedata{1} = data(1:index(1)-1,1:6);
for i = 1:length(index)-1
timedata{i+1} = data(index(i):index(i-1),1:6);
end
timedata{length(index)+1} = data(index(end):end,1:6);
  1 个评论
Frank Pernett
Frank Pernett 2021-3-9
Sorry for the late comment but this solution was also good, with a small change in the loop.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by