Scanning Matrix For Changes In Polarity

3 views (last 30 days)
Just a bit of background: I have a very large matrix (106740x5). I need to process this into smaller files with a certain format to be compatible with other software. I have figured out how to produce the files in the right format but I've had another problem.
Basically, I need to break the files up so that if the changes in one of the column values changes direction (for example:
  • 1
  • 5
  • 12
  • 2 - 2 is less than 12 which contradicts the pattern of the previous values being bigger). When a direction change does occur, I need to break the matrix at this point and output it to a text file (which I know how to do).
Breaking the problem down, I can think of 2 main things that I need to know what to do to solve this problem:
  • Identifying how many times that the column changes direction
  • Identifying where these changes occur
There are other issues which I may come back to but I think that if I can figure out these parts, I will be able to sort things out.

Accepted Answer

Robert
Robert on 23 Aug 2016
You could use diff and sign to create a logical array, then cumsum to convert that to a group index.
% where x is your column of data
ddx = diff(sign(diff(x)))~=0;
group = cumsum([1;0;ddx])
for ii = 1:group(end)
isinthisgroup = group == ii;
% your processing, file writing
end
  1 Comment
Harry Eggo
Harry Eggo on 23 Aug 2016
Thank you, Robert! This is exactly what I was looking for.

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!