Parsing a data set into multiple arrays based on value?

11 views (last 30 days)
Sorry I'm pretty new to matlab but...
I have a 1726x1 set of data that I would like to split into different variables. There should be 63 total variables and to find each variable I'd like to scan the original data set, once the value is larger than say 1000 I'd like to move that value and every value after it until the value drops below 1000 into a new variable. So if I have:
[10 100 1000 1005 11111000 55000 999 10 10000 10000 5000 6666 24]
I'd like it to split into 2 variables with
[1000 1005 11111000 55000 999] and
[10000 10000 5000 6666].
It should move through the whole data set and make 63 total variables when the values are larger than say 3000. Can anyone help?

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 2 Mar 2018
a = randi(10000,20,1); % Let a - your array
lo = a >= 1000;
ii = cumsum(diff([0;lo(:)]) == 1);
out = accumarray(ii(lo),a(lo),[],@(x){x});
  3 Comments
Stephen23
Stephen23 on 2 Mar 2018
"I want to then parse data2_reduced into 63 different variables with any name (A1, A2,... A63 or so)."
Don't do this. Magically creating or accessing variable names is how beginners force themselves into writing complex, slow, buggy code that is hard to debug. Read this to know more:
It would be simpler and much more efficient to use a cell array, exactly as Andrei Bobrov showed you.
Aron
Aron on 2 Mar 2018
I figured it out. Thank you both. You're right, I really appreciate the help.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating 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!