To find the number of sections with negative values

3 Ansichten (letzte 30 Tage)
aneps
aneps am 3 Mai 2016
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 Kommentare
CS Researcher
CS Researcher am 3 Mai 2016
Can you share the .dat file?
aneps
aneps am 3 Mai 2016
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)

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 3 Mai 2016
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 Kommentar
aneps
aneps am 3 Mai 2016
Bearbeitet: aneps am 3 Mai 2016
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..

Melden Sie sich an, um zu kommentieren.


Roger Stafford
Roger Stafford am 4 Mai 2016
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.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by