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

16 ビュー (過去 30 日間)
Thor Stenfjord
Thor Stenfjord 2019 年 9 月 20 日
回答済み: Walter Roberson 2019 年 9 月 21 日
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 件のコメント
the cyclist
the cyclist 2019 年 9 月 20 日
編集済み: the cyclist 2019 年 9 月 20 日
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

サインインしてコメントする。

回答 (2 件)

David Hill
David Hill 2019 年 9 月 20 日
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 2019 年 9 月 21 日
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

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by