Parsing a data set into multiple arrays based on value?

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?

 Respuesta aceptada

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 comentarios

Aron
Aron el 2 de Mzo. de 2018
Editada: Aron el 2 de Mzo. de 2018
THANK YOU!!
So I'm reading the data from a text file, removing the first column, and deleting values less than 10:
data1 = readtable('task2_data.txt');
data2 = data1{:,2};
data2_reduced = data2(data2(:,1) >= 10, :);
I want to then parse data2_reduced into 63 different variables with any name (A1, A2,... A63 or so).
"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 el 2 de Mzo. de 2018
I figured it out. Thank you both. You're right, I really appreciate the help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 2 de Mzo. de 2018

Editada:

el 2 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by