Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pkg load io;
- dat = readxls('TrafficAssignmentData.xlsx');
- % Q1
- fields = dat(1, :);
- vals = transpose(dat(2:end, :));
- question1 = cell2struct(vals, fields);
- % individual elements of the structure array
- % can be returned by indexing the variable like x(1)
- % end is a valid index expression
- % which refers to the last index in a
- % particular dimension
- output.Q1.expression1 = question1(end);
- output.Q1.expression2 = question1(1).UserClass;
- % a string value can be indexed for its characters
- output.Q1.expression3 = question1(2).UserClass(1);
- output.Q1.expression4 = question1(2).Volume;
- % 5
- % expression is not valid
- % structure array returns a comma separated list of values
- % if indexed by one of its own field names.
- % cs-lists cannot be indexed.
- % it is possible though to create a cell array from a cs-list
- % and index it as in:
- % {question1.Volume}(1)
- output.Q1.expression6 = strcmp(question1(1).Destination, '1086: CampNou');
- % one of the arguments of strcmp is a cell array
- % the other one is scalar
- % an array of the same size as the cell array is returned
- % containing the odjoints of string comparisons for each
- % member of the cell array
- output.Q1.expression7 = strcmp({question1.Destination}, '1086: CampNou');
- % [question1.Cost] converts cs-list into a matrix
- % comparison applied to a matrix produces a logical array (row vector)
- % find returns indices of non-zero values in the array (row vector)
- output.Q1.expression8 = find([question1.Cost] >= 30);
- odjoint = struct("Origin", {},
- "Destination", {},
- "Count", {});
- for i = 1:length(question1)
- ta = question1(i);
- test1 = strcmp({odjoint.Origin}, ta.Origin);
- test2 = strcmp({odjoint.Destination}, ta.Destination);
- test = test1 & test2;
- if (length(test) == 0 || !test)
- odjoint(length(odjoint) + 1) = struct("Origin", ta.Origin,
- "Destination", ta.Destination,
- "Count", 1);
- continue;
- endif
- ind = find(test);
- odjoint(ind).Count = odjoint(ind).Count + 1;
- endfor
- if (true)
- for i=1:length(odjoint)
- printf ("%-15s %-15s %i\n",
- odjoint(i).Origin,
- odjoint(i).Destination,
- odjoint(i).Count);
- end
- endif
- % Q2
- appdata = readxls('SmartphoneAppData.xlsx');
- fields = appdata(1, :);
- vals = transpose(appdata(2:end, :));
- question2 = cell2struct(vals, fields);
- unique_users = unique([question2.user_id]);
- output.Q2.user_id_count = length(unique_users);
- output.Q2.trip_id = length(unique({question2.trip_id}));
- user = struct("id", {});
- for uid = unique_users
- n = length(user) + 1;
- user(n).id = uid;
- trips_to_add = question2(find([question2.user_id] == uid));
- user(n).trip = rmfield(trips_to_add,
- {"user_id", "persona_id"});
- endfor
- user_times = arrayfun(@(u) {u.id, length(u.trip)}, user, 'UniformOutput', false);
- [output.Q2.maxtrips, maxind] = max(arrayfun(@(ut) ut{1}{2}, user_times));
- output.Q2.maxtripsuser = user(maxind).id;
- % Q3
- % adding item labels to pie chart
- % www.mathworks.com/help/matlab/creating_plots/customize-pie-chart-labels.html
- compliance_types = num2cell(unique([question2.compliance]));
- persona_types = unique({question2.persona_id});
- count_compliances = @(data, compliance) length(find([data.compliance] == compliance));
- filter_by_persona_type = @(data, persona_type) data(strcmp({data.persona_id}, persona_type));
- figure('name', 'compliance by persona type pie charts');
- for i=1:length(persona_types)
- pt = persona_types{i};
- % hold on
- subplot(2,2,i);
- h = pie( ...
- arrayfun(@(ct) ...
- count_compliances(filter_by_persona_type(question2, pt), ct{1}),
- compliance_types) ...
- );
- % fix pie chart labels
- hText = findobj(h, 'Type', 'text');
- percentValues = get(hText, 'String');
- txt = arrayfun(@(ct) cellstr(num2str(ct{1})), compliance_types )';
- combinedtxt = strcat({"Compliance "}, txt, {": "}, percentValues);
- oldExtents_cell = get(hText, 'Extent');
- oldExtents = cell2mat(oldExtents_cell);
- for i=1:length(hText)
- set(hText(i), 'String', combinedtxt(i, :));
- endfor
- % set(hText, {'String'}, combinedtxt);
- newExtents_cell = get(hText, 'Extent');
- newExtents = cell2mat(newExtents_cell); % numeric array
- width_change = newExtents(:,3)-oldExtents(:,3);
- signValues = sign(oldExtents(:, 1));
- offset = signValues.*(width_change/3);
- textPositions_cell = get(hText,{'Position'}); % cell array
- textPositions = cell2mat(textPositions_cell); % numeric array
- textPositions(:,1) = textPositions(:,1) + offset; % add offset
- for i=1:length(hText)
- set(hText(i), 'Position', textPositions(i, :));
- endfor
- title(strcat({"Persona type "}, pt));
- endfor
- legend("gopniks")
- figure('name', 'departure times per hour histogram');
- start_times = arrayfun(@(d) datevec(d.trip_start), question2, 'UniformOutput', false);
- start_hour = arrayfun(@(st) st{1}(4), start_times, 'UniformOutput', false);
- hist(cell2mat(start_hour), 1:1:24);
- xlabel("day hour");
- ylabel("# of departures");
- grid on
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement