필터 지우기
필터 지우기

Define new variables to each instance of a string of repetitive values

조회 수: 1 (최근 30일)
I would like to create a variable for each instance where I have a series of uninterrupted, repeated values. For example, one of my data columns is a behavioral code timeseries where 0 is when an animal is on the surface and 1 is when the animal is underwater (see below). I'd like to obtain the indices of the first dive (in this case (5:8)), the second dive (in this case (16:19)), the third dive, etc. so that I can plot each dive v. time elapsed, make these separate variables, and run analyses on each dive.
dive = [0
0
0
0
1
1
1
1
0
0
0
0
0
0
0
1
1
1
1
0
0
0]
Thanks in advance for your help.
  댓글 수: 3
Jessica Kendall-Bar
Jessica Kendall-Bar 2018년 6월 27일
I just found Jan's "RunLength" function, which seems like it would be a good solution, but I am a little confused on how to implement it.
Jessica Kendall-Bar
Jessica Kendall-Bar 2018년 6월 27일
Oh I just saw this, Paolo. No, I expect each dive to be a different length.

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

채택된 답변

Matt Macaulay
Matt Macaulay 2018년 6월 28일
편집: Matt Macaulay 2018년 6월 28일
The RunLength function on file exchange would be great for this. You could use it like this on the dive variable in the question:
[B, N, IB] = RunLength(dive);
% IB is an array of the dive start times (indexes)
IB = IB(B>0);
% N is an array of the dive lengths
N = N(B>0);
% Plot the dive start times against the dive lengths
plot(IB, N, 'kx')
xlabel('Start Time')
ylabel('Dive length')
  댓글 수: 3
Stephen23
Stephen23 2018년 6월 28일
"I've never downloaded and used a function before"
  • Click the big blue Download button.
  • Unzip it onto the MATLAB Search Path (e.g. the current directory).
If the submission contains MATLAB code then it is ready to use. If it contains some mex code (as this submssion does) then this must be compiled for your specific PC: Jan Simon normally writes some self-compiling code, that automagically compiles the first time it is called.
Jessica Kendall-Bar
Jessica Kendall-Bar 2018년 6월 28일
Thanks, I had downloaded it, but hadn't included it in the current directory. I just tried it and it worked!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 6월 28일
mask = [false, dive.', false];
starts = strfind(mask, [0 1]);
stops = strfind(mask, [1 0]) - 1;
divetimes = arrayfun(@(startidx, stopidx) times(startidx:stopidx), 'uniform', 0);
Now starts is a vector of the indices at which each dive started, and stops is a vector of corresponding indices at which each dive stopped. It is assumed that before and after the recorded information that they are above water.
divetimes() will be a cell array of times extracted from your times vector -- for example you might use this if your recordings are not at uniform time intervals.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by