Plotting data in table after grouping

6 ビュー (過去 30 日間)
Abed Alnaif
Abed Alnaif 2020 年 1 月 3 日
コメント済み: Adam Danz 2020 年 1 月 7 日
Hello,
I'm trying to plot data in a table after grouping it. If possible, I'd like to avoid using for loops with repeated 'plot' calls, since I think it would be slow. See below for an example of what I'm trying to do. Is there a way to make this code more efficient (e.g., avoid using for loops, or make a single 'plot' call)?
Thank you,
Abed
tbl = table([1;1;1;2;2;2;2;3;3],[0;1;4;0;2;3;5;1;4],rand(9,1),'VariableNames',{'id','t','x'});
uniqueIDs = unique(tbl.id);
figure;
hold on;
for iID = 1:length(uniqueIDs)
t = tbl.t(tbl.id==uniqueIDs(iID));
x = tbl.x(tbl.id==uniqueIDs(iID));
plot(t,x);
end

採用された回答

Adam Danz
Adam Danz 2020 年 1 月 3 日
編集済み: Adam Danz 2020 年 1 月 6 日
arrayfun() is one way to do that.
tbl = table([1;1;1;2;2;2;2;3;3],[0;1;4;0;2;3;5;1;4],rand(9,1),'VariableNames',{'id','t','x'});
figure();
hold on
arrayfun(@(i)plot(tbl.t(tbl.id==i),tbl.x(tbl.id==i)),unique(tbl.id)) % do plotting
  2 件のコメント
Abed Alnaif
Abed Alnaif 2020 年 1 月 6 日
Hi Adam,
Thanks for your answer.
Abed
Adam Danz
Adam Danz 2020 年 1 月 7 日
Glad I could help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by