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

조회 수: 16 (최근 30일)
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 CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by