Hello i have an array which look like in the photo bellow. every contactID has one or many activities(which are listed on the next rows under an empty contact id).
for example in line 15's contact id we have one activity and the next activity is at line 16.
how can i make an array of "contact_id"'s and in every cell we will have a sub array which adds all the activities if the following empty "contact id cell" as one array for this contact id?
thanks
mark |

 Respuesta aceptada

Robert
Robert el 7 de Nov. de 2017
In general, your task will be easier if you start by filling in those blank IDs. I will use data to refer to your table.
%%Fill in blanks (assumes first row is not blank)
for ii = 1:height(data)
ID = data.ContactID{ii};
if ~isempty(ID) % First occurance
prev = ID;
else % Not first occurance
data.ContactID{ii} = prev;
end
end
Then you can gather the IDs and select table subsets by common ID
[ID, ~, index] = unique(data.ContactID);
grouped = table();
grouped.ContactID = ID;
grouped.ActivityData = cell(height(grouped), 1);
for ii = 1:length(ID)
grouped.ActivityData{ii} = data(index == ii, 2:end);
end
You could also do this in a containers.Map object instead of a table.
grouped = containers.Map();
for ID = unique(data.ContactID)'
grouped(ID{1}) = data(strcmp(data.ContactID, ID{1}), 2:end);
end

5 comentarios

fima v
fima v el 7 de Nov. de 2017
Editada: fima v el 7 de Nov. de 2017
Hello,my file is xls file which i imported as shown in the photo bellow,how can i change the import so it will match the 'data' variable type which you presented?
i have attache the xls file.
Thanks
@Robert,
Your first part (filling the blank) can be replaced by just one line using fillmissing:
data = fillmissing(data, 'previous', 'DataVariables', 1)
The second part can also be replaced by just one line, using rowfun. Granted, it's a bit obscure:
grouped = rowfun(@(varargin) {table(varargin{:}, 'VariableNames', data.Properties.VariableNames(2:end))}, data, 'GroupingVariables', 1, 'InputVariables', 2:width(data))
@fima v,
Use readtable instead of the import tool
Robert
Robert el 7 de Nov. de 2017
@Guillaume
Thanks for mentioning fillmissing; very cool! I am using R2016a most of the time, which is one release too old for fillmissing.
fima v
fima v el 7 de Nov. de 2017
Editada: fima v el 7 de Nov. de 2017
Hello ,how its possible keep only the latest activity which has the highest date among them , for each contact ?
Thanks
Robert
Robert el 16 de Nov. de 2017
You should take a look at the second output of unique . It gives you the index of the first occurrence of each unique value. Specify that you want the 'last' occurrence and or sort your data by date as needed.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 7 de Nov. de 2017

Comentada:

el 16 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by