Indexing to return segments

조회 수: 3 (최근 30일)
Frank Pernett
Frank Pernett 2021년 1월 29일
댓글: Frank Pernett 2021년 3월 9일
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
Frank Pernett
Frank Pernett 2021년 1월 30일
Thank you so much!
It ran perfectly

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

추가 답변 (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에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by