constant variable
2 次查看(过去 30 天)
显示 更早的评论
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
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 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!