{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

structSort

# structSort - of the min function For reference here is the...

This preview shows page 1. Sign up to view the full content.

Sheet1 Page 1 %========================================================================== % Problem 3, part B. Sorting the structure array %-------------------------------------------------------------------------- % % Problem Statment: % % Write a function named structSort that takes in a structure array and a % string. The string specifies the name of a field in the structure % array that will only contain numeric, scalar values. You function % should return the structure array sorted based on the specified field. % The returned array should be sorted in ascending order (smallest to % largest) based on the values of the given field. % % Note: your algorithm should be very similar to the algorithm given to % you on problem 7 of homework 4 to sort a vector. The primary % difference is that you are sorting structures instead of numbers. This % means you will have to use the minStruct function from part A instead
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: % of the min() function. For reference, here is the algorithm from % homework 4 that was used to sort vectors: % % 1) Intialize the output vector to be an empty vector. % 2) Set up a loop to have a number of iterations equal to the % length of the input vector. In other words, if the length of the % input vector was 8, you would need to loop (for/while) 8 times. % For each iteration of the loop, do the following: % a) Find the minimum value and its position in the input vector. % Use the min() function. % b) Concatenate the minimum value to the end of the % output vector. % c) Use the position of this minimum value to remove it from the % input vector. % function S = structSort(A,str) a = A [r c] = size(A) vec = for y = 1:c [v loc] = minStruct(a,str) vec = [vec v] a(:,loc) = [r c] = size(a) end S = vec...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online