# L21post - Previous Lecture: Working with large data files...

This preview shows pages 1–13. Sign up to view the full content.

± Previous Lecture: ± Working with large data files ± Today’s Lecture: ± A data file example; built-in sort function ± Review matrix, cell array, structure array ± Announcement: ± Section in classrooms this week ± Prelim 2 tonight at 7:30pm ± Lastnames A - I: Hollister B14 ± Lastnames J - P: Kimball B11 ± Lastnames Q - Z: Upson B17

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
November 10, 2009 Lecture 21 2 A detailed sort-a-file example Suppose each line in the file statePop.txt is structured as follows: Cols 1-14: State name Cols 16-24: Population (millions) The states appear in alphabetical order.
November 10, 2009 Lecture 21 3 Alabama 4557808 Alaska 663661 Arizona 5939292 Arkansas 2779154 California 36132147 Colorado 4665177 : : : : Texas 22859968 Utah 2469585 Vermont 623050 Virginia 7567465 Washington 6287759 West Virginia 1816856 Wisconsin 5536201 Wyoming 509294

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
November 10, 2009 Lecture 21 4 A detailed sort-a-file example Create a new file statePopSm2Lg.txt that is structured the same as statePop.txt except that the states are ordered from smallest to largest according to population. • Need the pop as numbers for sorting. • Can’t just sort the pop— have to maintain association with the state names. Alabama 4557808 Alaska 663661 Arizona 5939292 Arkansas 2779154 California 36132147 Colorado 4665177 : : : :
November 10, 2009 Lecture 21 5 First, get the populations into an array C = file2cellArray('StatePop'); n = length(C); pop = zeros(n,1); for i=1:n S = C{i}; pop(i) = str2double(S(16:24)); end

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
November 10, 2009 Lecture 21 6 Built-In function sort Syntax: [y,idx] = sort(x) 10 20 5 90 15 5 10 15 20 90 3 1 5 2 4 X: y: idx: y(1) = x(3) = x(idx(1))
November 10, 2009 Lecture 21 7 Built-In function sort Syntax: [y,idx] = sort(x) 10 20 5 90 15 5 10 15 20 90 3 1 5 2 4 X: y: idx: y(2) = x(1) = x(idx(2))

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
November 10, 2009 Lecture 21 8 Built-In function sort Syntax: [y,idx] = sort(x) 10 20 5 90 15 5 10 15 20 90 3 1 5 2 4 X: y: idx: y(3) = x(5) = x(idx(3))
November 10, 2009 Lecture 21 9 Built-In function sort Syntax: [y,idx] = sort(x) 10 20 5 90 15 5 10 15 20 90 3 1 5 2 4 X: y: idx: y(4) = x(2) = x(idx(4))

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
November 10, 2009 Lecture 21 10 Built-In function sort Syntax: [y,idx] = sort(x) 10 20 5 90 15 5 10 15 20 90 3 1 5 2 4 X: y: idx: y(5) = x(4) = x(idx(5))
November 10, 2009 Lecture 21 11 Built-In function sort Syntax: [y,idx] = sort(x) 10 20 5 90 15 5 10 15 20 90 3 1 5 2 4 X: y: idx: y(k) = x(idx(k))

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
November 10, 2009 Lecture 21 12
This is the end of the preview. Sign up to access the rest of the document.

## L21post - Previous Lecture: Working with large data files...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online