Clear Filters
Clear Filters

I have 2 columns, names and associated events, which have a many to many relationship. How do I get a matrix with first element of each row the event and subsequent elements the event attendees (NAN filling when an event had fewer attendees than max)

2 views (last 30 days)
Many to Many meaning an individual has attended many events, and events have been attended by many individuals.
For example, if numbers are events and letters are people:
R=["1", "a"; "4", "a"; "5", "a"; "7", "b"; "5", "b"; "4", "b"; "7", "c"]
I would like to get it to the following result
P = ["1", "a", "NaN"; "4", "a", "b"; "5", "a","b"; "7", "b", "c"]
Notice only person a attended event 1 and NaN is filled in.
I can sort my nx2 matrix by event so that I can see who went to each event, but I am unable to make events unique such that there is one row for each event and the names next to it. The number of columns will have to be the column for events plus the max number of people who attended a single event (in the example above 2), with any event with attendance less than max filled with NaN so that there are no dimension issues with the matrix.
Thank you for any help.

Accepted Answer

Shadaab Siddiqie
Shadaab Siddiqie on 6 Oct 2020
From my understanding you want to create a matrix with all the unique elements from the first row (events) with there corosponding elements in that coloum (people). Here is the code that might help:
R=["1", "a"; "4", "a"; "5", "a"; "7", "b"; "5", "b"; "4", "b"; "7", "c"];
% find all the unique events
uniqueCol = unique(R(:,1));
a = [];
%find different persons at each events
for x = 1:length(uniqueCol)
t = R(find(R==uniqueCol(x)),2)';
% argesting the size of the array
t = [t NaN(2-length(t))];
a = [a;t];
end
Answer = [uniqueCol a];

More Answers (0)

Community Treasure Hunt

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

Start Hunting!