How can I create multiple arrays, based on unique column values, from a single array?

16 visualizaciones (últimos 30 días)
I have a large array that I would like to separate into different arrays, based on the "ID" value, so I can plot Temp vs Speed for each ID.
What kind of loop would I use? I've included a sample of my data below.
ID Speed Temp
232459 120 80
232459 240 150
232459 100 70
217220 150 85
217220 130 82
217220 100 71
217220 101 72
217220 110 81
217220 100 74
195321 110 84
It would also be nice to have each array be named the "ID Number"
  1 comentario
the cyclist
the cyclist el 20 de Sept. de 2019
Editada: the cyclist el 20 de Sept. de 2019
How are these data stored? Are they in a numerical array (and you simply wrote the column header here for our convenience), or are they stored in a table? Or something else?
Also, do you really need these stored when all is said and done, or could your loop be something like ...
for <each ID>
% create temporary variables for this ID's speed and temp
% plot a figure using that temporary variable
end

Iniciar sesión para comentar.

Respuestas (2)

David Hill
David Hill el 20 de Sept. de 2019
For your array x:
function Plot(x)
hold on;
z=x(:,1);
arrayfun(@(y)plot(x(ismember(z,y),3),x(ismember(z,y),2),'Marker','*','MarkerSize',10,'LineStyle','none'),unique(z));
end

Walter Roberson
Walter Roberson el 21 de Sept. de 2019
G = findgroups(YourTable.ID);
grouped_info = splitapply(@(speed, temp) {unique(id), speed, temp}, YourTable.id, YourTable.Speed, YourTable.Temp, G);
This will return a cell array of information, with 3 columns. The first column contains (one copy of) the ID that applies for the row. The second column contains the speeds that existed for that ID. The third column contains the corresponding temperatures that existed for that ID.
You could even plot fairly directly:
G = findgroups(YourTable.ID);
hold on
line_handles = splitapply(@(id, speed, temp) plot(speed, temp, 'DisplayName', unique(id)), YourTable.id, YourTable.Speed, YourTable.Temp, G);
hold off
legend show

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by