How to find values in a range and save them to different arrays, repeatedly

16 visualizaciones (últimos 30 días)
Hi all,
I am curious to see if there is a better way to what I am attempting below, in as few lines as possible. I have a column vector with the stimulus times ranging from 0 to 120,000 milliseconds. I would like to divide this vector up into 10,000 millisecond tranches, either in a new vector, a field in a structure, etc. So these tranches would be all the values 0-9,999ms, 10,000-20,0000, all the way to 120,000. My current code (shown below) is long and pretty crude and I'm sure there are quicker, more reliable, and more elegant ways to do this.
target_times= randi(120000,1,64) %imagine that this is my array with times, 64 different time values from 0-120000
JSON1=[0];
JSON2=[]
JSON3=[];
... %i have to create a bunch of blank arrays, which will be my ten second tranches.
% i cant use zeros because the size of these arrays will vary based on chance. this is ugly and I'd like to get rid of it
JSON12=[];
%evaluates if the value indexed is within a certain range and writes it to the appropriate file. I would really like to get rid of all these elifs!
for i=1:length(target_times)
if target_times(i)< 10000
JSON1_targets=[JSON1_targets,target_times(i)];
elseif (target_times(i) < 20000) && (target_times(i) >= 10000)
JSON2_targets=[JSON2_targets,target_times(i)];
elseif (target_times(i) < 30000) && (target_times(i) >= 20000)
JSON3_targets=[JSON3_targets,target_times(i)];
...
elseif (target_times(i) < 120000) && (target_times(i) >= 110000)
JSON12_targets=[JSON12_targets,target_times(i)];
end
end
So if anyone has a good idea about how to separate all the values based on what 10,000 ms range they fall in, without having to predefine 12 blank arrays and write 12 different elif statements, I would appreciate it!
  2 comentarios
Stephen23
Stephen23 el 10 de Dic. de 2019
Using numbered variables is a sign that you are doing something wrong.
Using a cell array and indexing would make your task easy.
Steven Lord
Steven Lord el 10 de Dic. de 2019
There may be an even easier way to accomplish your goal than breaking your data set up into smaller pieces, depending on what your goal is. [Some potential tools include retime, groupsummary, grouptransform, etc.] Can you say a little more about what you are planning to do with these tranches?

Iniciar sesión para comentar.

Respuesta aceptada

Jeremy
Jeremy el 10 de Dic. de 2019
Editada: Jeremy el 10 de Dic. de 2019
target_times= randi(120000,1,64);
bins = 10000:10000:120000;
c = cell(12,1);
c{1} = target_times(target_times < bins(1));
for i = 2:length(bins)
c{i} = target_times(target_times < bins(i) & target_times >= bins(i-1));
end
You can access the values in each bin using curly brackets. E.g. c{3}

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by