constant variable

조회 수: 2 (최근 30일)
justin
justin 2012년 3월 16일
I have a matrix A=data where data contains (10000x1) values. I want to write a command that will capture the location, value and length when the same value repeats five or more times in a row excluding 0. ex. 1 0 3 4 4 4 4 0 2 3 5 5 5 5 5 5 7 3 2 0 0 0 0 0 1 1 1 1 1 0 It would capture the 5 displaying: length - 6, location - 11, and value - 5; it would also caputre 1 displaying: length - 5, location - 25, and value - 0.
It would exclude the 0's at location - 20 because the value is 0. I tried to write the following code but it doesn't cover all the parameters I am looking for:
A = data';
[M,V] = regexp(sprintf('%i',[0 diff(A)==0]),'1+','match');
[M,I] = max(cellfun('length',M));
M = M + 1 %length
I = V(I) - 1 % location
V = A(I) %value
I would like to have an output of showing B = (11:16,25:29) so I can compare it to A.
As well I have one more question. I have another code where so info(10000x3) where info(:,1) = time, info(:,2) = data1 and info(:,3)=data2. Is there I can write a command that says when info(;,2)<100 & ~=0 and when info(:,3) increases by 20; these both occur at the same time an output of info(:,1) at this locations will be display.
Any help on this would be greatly appreciated.
Thanks
  댓글 수: 1
Jan
Jan 2012년 3월 16일
Please post only one question per thread.
Does the code you are posting for the first problem answer your question already?!

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

답변 (2개)

justin
justin 2012년 3월 19일
I'm sorry, referring to the first question the code does not answer my question. It only gives me a max constant and includes 0. I would like to find all the constants that are atleast 5 or more in a row and not 0. Thanks

Jonathan Sullivan
Jonathan Sullivan 2012년 3월 19일
Justin. You'll want a very handy function, findseq. It can be downloaded on the file exchange. http://www.mathworks.com/matlabcentral/fileexchange/28113-findseq
The code you'll have to use is as follows:
[vals, start, stop, len] = findseq(A);
len(vals == 0) = -inf; % Exclude 0s
[~,ind] = max(len); % Find the longest one.
vals(ind) % Report the value
start(ind) % Report the start index
stop(ind) % Report the stop index
len(ind) % Report the length

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by