To find the number of sections with negative values

조회 수: 3 (최근 30일)
aneps
aneps 2016년 5월 3일
답변: Roger Stafford 2016년 5월 4일
I have data in the form of a matrix with 3 columns. On the third column it is the velocities. A sample look like this:
1 3 5
2 5 8
3 7 6
4 8 -1
5 9 -3
6 8 -1
7 9 1
8 5 2
....etc
It is a data file (.dat). How can I program in Matlab so that the code look for negative values from the beginning of the 3rd column (velocity). When it see first negative value on the 3rd column, check if it is negative for at least next consecutive 5 rows. If at least 5 consecutive rows are (only third column) are negative, take the whole consecutive rows with negative values as 'section1'. Then again look where the next negative value starts, if it find next negative value, again check if atleast next 5 consecutive rows have negative values, if yes, take the whole consecutive negative values as 'section2'..... such way count " how many sections are there?".
PS: Each section may have more than 5 (may be 8 or 10 or even more) consecutive negative values rows. Just need to make sure at least 5 consecutive rows are negative. Thank you.
Sample data is attached. Here, first column is the number (in my case ions), second is Vx, third Vy and fourth Vz. I want to check if the Vy reverses or not (the third column)
  댓글 수: 2
CS Researcher
CS Researcher 2016년 5월 3일
Can you share the .dat file?
aneps
aneps 2016년 5월 3일
Yes, I have attached...Here, first column is the number (in my case ions), second is Vx, third Vy and fourth Vz. I want to check if the Vy reverses or not (the third column)

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

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 3일
a=rand(100,3)-0.6 % Example
c3=a(:,3);
dd=(c3<0)';
f=cumsum(~dd)+dd(1);
ff=f.*dd
[~,~,kk]=unique(ff)
id=accumarray(kk,(1:numel(kk))',[],@(x) numel(x))
id(1)=0
b=accumarray(kk,(1:numel(kk))',[],@(x) {a(x,:)})
out=b(id>=5)
celldisp(out)
  댓글 수: 1
aneps
aneps 2016년 5월 3일
편집: aneps 2016년 5월 3일
Is it possible to get the result in comparison with the first column? I mean, in the example file attached (Sampledata.txt), the first column has numbers 1, 2 and 3... how many sections correspond to 1, how many corresponds to 2... so on.. it would be good if the result printed in two columns saying 1 has this many sections, 2 has this many sections etc..

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


Roger Stafford
Roger Stafford 2016년 5월 4일
Let x be the third column of your data matrix.
f = find(diff([false;x<0;false])~=0);
ns = length(find(f(2:2:end)-f(1:2:end-1)>=5)); % Total number of sections
Note: This is a solution to your originally stated problem. In the revised version, as given in your comment, I can’t tell whether, say, several 1’s in the first column separated by another value and then returning later to 1’s would count as “consecutive” 1’s if all the 1’s were accompanied by negative values in the third column. I think you need to resolve that uncertainty and open up a new ‘Answers’ question for that.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by