Advertisement
Guest User

Untitled

a guest
Dec 29th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 4.90 KB | None | 0 0
  1. pkg load io;
  2. dat = readxls('TrafficAssignmentData.xlsx');
  3. % Q1
  4.  
  5. fields = dat(1, :);
  6. vals = transpose(dat(2:end, :));
  7. question1 = cell2struct(vals, fields);
  8.  
  9. % individual elements of the structure array
  10. % can be returned by indexing the variable like x(1)
  11.  
  12. % end is a valid index expression
  13. % which refers to the last index in a
  14. % particular dimension
  15.  
  16. output.Q1.expression1 = question1(end);
  17.  
  18. output.Q1.expression2 = question1(1).UserClass;
  19.  
  20. % a string value can be indexed for its characters
  21. output.Q1.expression3 = question1(2).UserClass(1);
  22.  
  23. output.Q1.expression4 = question1(2).Volume;
  24.  
  25. % 5
  26.  
  27. % expression is not valid
  28. % structure array returns a comma separated list of values
  29. % if indexed by one of its own field names.
  30. % cs-lists cannot be indexed.
  31. % it is possible though to create a cell array from a cs-list
  32. % and index it as in:
  33. % {question1.Volume}(1)
  34.  
  35.  
  36. output.Q1.expression6 = strcmp(question1(1).Destination, '1086: CampNou');
  37.  
  38.  
  39. % one of the arguments of strcmp is a cell array
  40. % the other one is scalar
  41. % an array of the same size as the cell array is returned
  42. % containing the odjoints of string comparisons for each
  43. % member of the cell array
  44. output.Q1.expression7 = strcmp({question1.Destination}, '1086: CampNou');
  45.  
  46.  
  47. % [question1.Cost] converts cs-list into a matrix
  48. % comparison applied to a matrix produces a logical array (row vector)
  49. % find returns indices of non-zero values in the array (row vector)
  50. output.Q1.expression8 = find([question1.Cost] >= 30);
  51.  
  52. odjoint = struct("Origin", {},
  53.     "Destination", {},
  54.     "Count", {});
  55.  
  56. for i = 1:length(question1)
  57.     ta = question1(i);
  58.     test1 = strcmp({odjoint.Origin}, ta.Origin);
  59.     test2 = strcmp({odjoint.Destination}, ta.Destination);
  60.     test = test1 & test2;
  61.     if (length(test) == 0 || !test)
  62.         odjoint(length(odjoint) + 1) = struct("Origin", ta.Origin,
  63.     "Destination", ta.Destination,
  64.     "Count", 1);
  65.         continue;
  66.     endif
  67.  
  68.     ind = find(test);
  69.     odjoint(ind).Count = odjoint(ind).Count + 1;
  70. endfor
  71.  
  72.  
  73. if (true)
  74.     for i=1:length(odjoint)
  75.         printf ("%-15s %-15s %i\n",
  76.             odjoint(i).Origin,
  77.             odjoint(i).Destination,
  78.             odjoint(i).Count);
  79.     end
  80. endif
  81.  
  82.  
  83. % Q2
  84.  
  85. appdata = readxls('SmartphoneAppData.xlsx');
  86. fields = appdata(1, :);
  87. vals = transpose(appdata(2:end, :));
  88. question2 = cell2struct(vals, fields);
  89.  
  90. unique_users = unique([question2.user_id]);
  91. output.Q2.user_id_count = length(unique_users);
  92. output.Q2.trip_id = length(unique({question2.trip_id}));
  93.  
  94. user = struct("id", {});
  95.  
  96. for uid = unique_users
  97.     n = length(user) + 1;
  98.     user(n).id = uid;
  99.     trips_to_add = question2(find([question2.user_id] == uid));
  100.  
  101.     user(n).trip = rmfield(trips_to_add,
  102.         {"user_id", "persona_id"});
  103. endfor
  104.  
  105. user_times = arrayfun(@(u) {u.id, length(u.trip)}, user, 'UniformOutput', false);
  106. [output.Q2.maxtrips, maxind] = max(arrayfun(@(ut) ut{1}{2}, user_times));
  107. output.Q2.maxtripsuser = user(maxind).id;
  108.  
  109.  
  110. % Q3
  111.  
  112. % adding item labels to pie chart
  113. % www.mathworks.com/help/matlab/creating_plots/customize-pie-chart-labels.html
  114.  
  115. compliance_types = num2cell(unique([question2.compliance]));
  116. persona_types = unique({question2.persona_id});
  117. count_compliances = @(data, compliance) length(find([data.compliance] == compliance));
  118. filter_by_persona_type = @(data, persona_type) data(strcmp({data.persona_id}, persona_type));
  119.  
  120. figure('name', 'compliance by persona type pie charts');
  121.  
  122. for i=1:length(persona_types)
  123.     pt = persona_types{i};
  124.     % hold on
  125.     subplot(2,2,i);
  126.     h = pie( ...
  127.         arrayfun(@(ct) ...
  128.             count_compliances(filter_by_persona_type(question2, pt), ct{1}),
  129.             compliance_types) ...
  130.         );
  131.     % fix pie chart labels
  132.     hText = findobj(h, 'Type', 'text');
  133.     percentValues = get(hText, 'String');
  134.     txt = arrayfun(@(ct) cellstr(num2str(ct{1})), compliance_types )';
  135.     combinedtxt = strcat({"Compliance "}, txt, {": "}, percentValues);
  136.     oldExtents_cell = get(hText, 'Extent');
  137.     oldExtents = cell2mat(oldExtents_cell);
  138.     for i=1:length(hText)
  139.         set(hText(i), 'String', combinedtxt(i, :));
  140.     endfor
  141. %   set(hText, {'String'}, combinedtxt);
  142.     newExtents_cell = get(hText, 'Extent');
  143.     newExtents = cell2mat(newExtents_cell); % numeric array
  144.     width_change = newExtents(:,3)-oldExtents(:,3);
  145.     signValues = sign(oldExtents(:, 1));
  146.     offset = signValues.*(width_change/3);
  147.  
  148.     textPositions_cell = get(hText,{'Position'}); % cell array
  149.     textPositions = cell2mat(textPositions_cell); % numeric array
  150.     textPositions(:,1) = textPositions(:,1) + offset; % add offset
  151.  
  152.     for i=1:length(hText)
  153.         set(hText(i), 'Position', textPositions(i, :));
  154.     endfor
  155.    
  156.    
  157.  
  158.     title(strcat({"Persona type "}, pt));
  159.  
  160. endfor
  161. legend("gopniks")
  162.  
  163. figure('name', 'departure times per hour histogram');
  164. start_times = arrayfun(@(d) datevec(d.trip_start), question2, 'UniformOutput', false);
  165. start_hour = arrayfun(@(st) st{1}(4), start_times, 'UniformOutput', false);
  166. hist(cell2mat(start_hour), 1:1:24);
  167. xlabel("day hour");
  168. ylabel("# of departures");
  169. grid on
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement