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

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 commentaire

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

Connectez-vous pour commenter.

Réponses (2)

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
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

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by