indexing to different table
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Time ICAO age A ID HPL FLG
'12:59:45.466' 'OMA812' 0.50000000 5 1907317 135 228
'12:59:39.483' 'OMA812' 12.500000 5 1906896 135 228
'12:59:39.483' 'OMA812' 9.1999998 5 1906502 131 228
'12:59:39.483' 'OMA812' 8.5000000 5 1906421 131 228
'12:59:49.306' 'OMA812' 6.5999999 5 1907976 127 228
'12:59:39.483' 'OMA812' 7.5999999 5 1906277 127 228
I have the following table above, I would like to break up the table by HPL number. Move data for HPL 135 to one table, 131 to another table and 127 to another.
I like to put that in a loop so I can perform that for more HPL ids.
so far I have this but does not work
hplid = [];
hplid = unique(Join.spf_hplId);
latency_plots = [];
data_array=[];
for k=1: length(hplid)
join_idx=Join.spf_hplId==hplid(k);
%Join(k)=Join(join_idx,:);
test.test=Join(join_idx,:);
data_array=[data_array;test];
end
Thank you
5 comentarios
Guillaume
el 18 de Sept. de 2017
Agree with Stephen, splitting the table into multiple tables sounds like a very bad idea, so explain why you want to do that.
If it's so you can calculate some properties/statistics per HPL number, then this can be done a lot more easily on the whole table with rowfun or splitapply, e.g. to calculate the mean age per HPL:
rowfun(@mean, yourtable, 'InputVariables', 'age', 'GroupingVariables', 'HPL')
Respuestas (2)
Steven Lord
el 18 de Sept. de 2017
Do you want to create individual variables with a name that somehow includes information about the HPL number it represents (in which case see Stephen's comment about dynamically named variables) or do you simply need to process each set of HPL values in turn? If the latter, use logical indexing.
% Create a sample table
rng default
n = 20;
X = randi([-10 10], n, 1);
Y = randi([0 4], n, 1);
processed = false(n, 1);
T = table(X, Y, processed);
% Get the list of unique values for the Y variable in the table
listOfUniqueY = unique(T.Y);
% Iterate over those unique values. The unique function returns a column vector
% but if I tranpose the result I can directly iterate over the values.
for k = listOfUniqueY.'
% Select those rows of T that have Y equal to each unique value
selectedRowsOfT = T.Y == k;
TforY = T(selectedRowsOfT, :);
% How many are there?
fprintf('T has %d rows with Y value %d.\n', height(TforY), k);
% Mark the selected rows as processed. Leave the semicolon off to display the table.
T{selectedRowsOfT, 'processed'} = true
end
Note I used the logical index vector both to extract data (to create TforY) and to assign (the line where I update the 'processed' variable.)
0 comentarios
Peter Perkins
el 21 de Sept. de 2017
It sounds like what you ultimately want is to compute statistics within groups on variables in a table. If that's correct, here's what I'd do:
>> t = table(randn(10,1),randi(2,10,1),'VariableNames',{'latency' 'HPL'})
t =
10×2 table
latency HPL
________ ___
-2.2584 1
2.2294 2
0.33756 1
1.0001 2
-1.6642 2
-0.59003 2
-0.27806 2
0.42272 1
-1.6702 2
0.47163 1
>> myStats = @(x) [min(x) median(x) max(x)];
>> tStats = varfun(myStats, t,'GroupingVariables','HPL')
tStats =
2×3 table
HPL GroupCount Fun_latency
___ __________ _______________________________
1 4 -2.2584 0.38014 0.47163
2 6 -1.6702 -0.43405 2.2294
That creates one summary stats variable in the output table, with three columns. That's probably good if you have more than one data variable. Or, you could use rowfun to do essentially the same thing but create three separate variables in the output. Or you could just split them up by hand:
>> tStats.minLatency = tStats.Fun_latency(:,1);
>> tStats.medianLatency = tStats.Fun_latency(:,2);
>> tStats.maxLatency = tStats.Fun_latency(:,3);
>> tStats.Fun_latency = []
tStats =
2×5 table
HPL GroupCount minLatency medianLatency maxLatency
___ __________ __________ _____________ __________
1 4 -2.2584 0.38014 0.47163
2 6 -1.6702 -0.43405 2.2294
0 comentarios
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!