Creating Arrays

1 / 50

Creating Arrays - PowerPoint PPT Presentation

Creating Arrays. Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4. Ex6 and Ex7. Use of matrices in real world. 1. 1. Creating scalars. Assign a value to a variable (i.e. Hardcode)

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

PowerPoint Slideshow about 'Creating Arrays' - cheung

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Creating Arrays

Creating scalars, vectors, matrices

Ex1 & 2. Dot Product & Cross Product

Ex3. Plotting Graphs

Ex4. Conversion Table

Ex5. Plotting functions

Finishing Ex4.

Ex6 and Ex7. Use of matrices in real world

1

1. Creating scalars
• Assigna value to a variable (i.e. Hardcode)

pressure = 10; %pascals

temperature = 298; %kelvin

• Store the result of an equation

pressure = density*R*temperature;

• Save the return-value of the input() command

age = input(‘Enter your age: ’);

2

2. Creating vectors
• There are LOTS of ways to create vectors, based on three simple ideas:
• The values in the vector are pre-defined. For example:

[ 2 -5 4.4 -96.6]

3

2. Creating vectors
• There are LOTS of ways to create vectors, based on three simple ideas:
• The values in the vector are pre-defined. For example:

[ 2 -5 4.4 -96.6]

• The values have a pattern (addition only). For example:

[10, 20, 30 ,…100] or [-10 -8 -6 -4 -2 0]

4

2. Creating vectors
• There are LOTS of ways to create vectors, based on three simple ideas:
• The values in the vector are pre-defined. For example:

[ 2 -5 4.4 -96.6]

• The values have a pattern (addition only). For example:

[10, 20, 30 ,…100] or [-10 -8 -6 -4 -2 0]

• Finally, the total amount of values is known. For example:

25 points evenly spaced from 0 to 100.

5

2.1. Pre-defined values, cont.

What else are semi-colons used for?

9

2.1. Pre-defined values, cont.

They create rows AND suppress output!

What else are semi-colons used for?

10

2.1. Pre-defined values, cont.

The apostrophe allows to transpose a vector. Rows become columns. Columns become rows.

They create rows AND suppress output!

What else are semi-colons used for?

11

2.1. Pre-defined values, cont.

The apostrophe allows to transpose a vector. Rows become columns. Columns become rows.

They create rows AND suppress output!

What dimension will speeds have? _______________________________

What else are semi-colons used for?

12

Ex1. Dot product
• Remember the DOT product? (maybe/maybe not)

Credits to: http://www.itee.uq.edu.au/~cogs2010/cmc/chapters/Hebbian/ten5.gif

The DOT product…

Ex1. Dot product
• Remember the DOT product? (maybe/maybe not)

Credits to: http://www.itee.uq.edu.au/~cogs2010/cmc/chapters/Hebbian/ten5.gif

The DOT product…

• In Matlab
*

*

*

*

*

*

Ex2. Cross product
• How about the CROSS product? (maybe/maybe not)

Source: Wikipedia

The CROSS product…

y

x y

-7

-2

3

8

4

-7

3

-1

x

Ex3. Plotting graphs
• In order to plot, Matlab needs data points:
y

x y

-7

-2

3

8

4

-7

3

-1

x

Ex3. Plotting graphs
• In order to plot, Matlab needs data points:
y

x y

-7

-2

3

8

4

-7

3

-1

x

Ex3. Plotting graphs
• In order to plot, Matlab needs data points:

Matlab connects the dots!

y

x y

-7

-2

3

8

4

-7

3

-1

x

Ex3. Plotting graphs
• In order to plot, Matlab needs data points:
• Well…
• x is an array of data points x = [-7 -2 3 8]
• y is another array of data points y = [4 -7 3 -1]
• …for the curious ones, to plot: plot(x,y)

The range operator

Numbers are separated by +1

21

2.2. Patterns, cont.

The range operator

Numbers are separated by +1

An additional value in the middle specifies the increment.

+3 +3 +3 +3 +3 +3 +3

+3 >32 

22

2.2. Patterns, cont.

The range operator

Numbers are separated by +1

An additional value in the middle specifies the increment.

Go reverse by using a negative increment! CAUTION: the beginning number must be > the end number. Here 10>3. (This also shows it works with decimals.)

+3 +3 +3 +3 +3 +3 +3

+3 >32 

-2.5 -2.5 -2.5 < 3 

23

2.2. Patterns, cont.

The range operator

Numbers are separated by +1

An additional value in the middle specifies the increment.

To use the apostrophe and create a column vector, absolutely place brackets first!

… else….

+3 +3 +3 +3 +3 +3 +3

+3 >32 

-2.5 -2.5 -2.5 < 3 

24

2.2. Patterns, cont.

The range operator

Numbers are separated by +1

An additional value in the middle specifies the increment.

To use the apostrophe and create a column vector, absolutely place brackets first!

… else….

Only the scalar -10 gets transposed: but a scalar transposed remains the same scalar!

+3 +3 +3 +3 +3 +3 +3

+3 >32 

-2.5 -2.5 -2.5 < 3 

25

Ex4. Conversion table

% create celsius data points

celsius = 0:10:100; %0 to 100 by +10 increment

% calculate Fahrenheit

fahrenheit = celsius * 9/5 + 32;

% show table

`2.3. Specific amount of data pointsA built-in function called linspace() spaces elements linearly in an array.What does this mean?The distance between each consecutive data point is equal.There are two ways to use it, as Matlab ‘hints’ when the command typed is unfinished:Either provide 2 arguments, or provide 3 arguments.27`
`2.3. linspace(), cont.The third argument indicates the ________________________ .28`
`2.3. linspace(), cont.The third argument indicates the ________________________ .When Matlab cannot display all the elements on one line, it simply indicates the column-number per line.29`
`2.3. linspace(), cont.The third argument indicates the ________________________ .When Matlab cannot display all the elements on one line, it simply indicates the column-number per line.30`
`2.3. linspace(), cont.?????? %no third argumentOmitthe third argument uses a default of _______ data points!`
`Ex5. Plotting graphsSuppose a function that relates each x to its y-coordinate is known: y = f(x) = x2.  Plot y vs. x.`
` x y-10-55101002525100Ex5. Plotting graphsSuppose a function that relates each x to its y-coordinate is known: y = f(x) = x2.  Plot y vs. x.In this case, it is tedious work to hard-code each x and y array. Are 4 data-points sufficient, like in example 3?yx`
`Ex5. Plotting f(x) = x^2, cont.Remember: which built-in function influences the number of data-points in an array?____________________In this case:%array x of 20 data points%calculate array of y’s.%plot command And the result is…`
`Ex5. Plotting f(x) = x^2, cont.Remember: which built-in function influences the number of data-points in an array?____________________In this case:%array x of 20 data pointsx = linspace(-10,10,20); %calculate array of y’s.y = x.^2; %(The dot will be explained next time…)%plot commandplot(x,y) And the result is…`
`Ex5. Plotting f(x) = x^2, cont.Does this represent f(x) = x2 ? Yes Or No Yes, but it took 20 points!!`
`Ex5. Plotting f(x) = x^2, cont.The use of linspace() in this example is crucial! Why do all 20 data point need to be linearly spaced? What would happen otherwise?Still 20 points!!.. but the first 19 are before -5,.. and the last one is 10.Not f(x) = x2..`
`3. Creating MatricesSimply a combinationof all symbols introduced with vectors!Square brackets [ ]Spaces or commas , ,Semi-colons ;Apostrophes ’38`
`3.1. Matrices: hard-codingUse semi-colons to create new rows. ONLY rectangular matrices:The number of columns MUST match for each row, and vice-versa.39`
`3.2. Reusing Previous matricesUse semi-colons to create new rows. ONLY rectangular matrices:The number of columns MUST match for each row, and vice-versa.Use previousmatrices to actually create new matrices.This example transposes the matrix variable a.40`
`3.3. Using ColonsUse semi-colons to create new rows. ONLY rectangular matrices:The number of columns MUST match for each row, and vice-versa.You can use previousmatrices to actually create new matrices.This example transposes the variable a.Combine any previous methods, AS LONG AS the matrix remains rectangular.41`
`3.4. “Concatenating”Use semi-colons to create new rows. ONLY rectangular matrices:The number of columns MUST match for each row, and vice-versa.Finally, create arrays by combining previous variables! This is called CONCATENATING.You can use previousmatrices to actually create new matrices.This example transposes the variable a.You can combine any previous methods, AS LONG AS the matrix remains rectangular.42`
`3.5. Using the command windowUse semi-colons to create new rows. ONLY rectangular matrices:The number of columns MUST match for each row, and vice-versa.When the array becomes too big, the numbers no longer display.You can use previousmatrices to actually create new matrices.This example transposes the variable a.You can combine any previous methods, AS LONG AS the matrix remains rectangular.43`
`Ex4. Conversion table, end!% create celsius data pointscelsius = 0:10:100; %0 to 100 by +10 increment% calculate Fahrenheitfahrenheit = celsius * 9/5 + 32;% show table[celsius’ fahrenheit’]`
`Ex6. Sling ThermometerA method to read relative-humidity. `
`Ex7. ImagesEach row and column have a pixel value stored.`
`Wrapping UpKnow by heart each way to create a row/column vector.Hard-code each data pointSeparate each data-point by comma or spaces for row vectorSeparate each data-point by semicolon for a column vectorShortcut when there is an addition pattern (colon)Shortcut when a specific amount of data points are linearly spaced (linspace())`
`Wrapping UpKnow by heart each way to create a row/column vector.Hard-code each data pointSeparate each data-point by comma or spaces for row vectorSeparate each data-point by semicolon for a column vectorShortcut when there is an addition pattern (colon)Shortcut when a specific amount of data points are linearly spaced (linspace())Realize that creating matrices only requires combining all of the above, while respecting one crucial rule:A matrix must remain rectangular at all times (i.e. no holes within the matrix)`
`Wrapping UpKnow by heart each way to create a row/column vector.Hard-code each data pointSeparate each data-point by comma or spaces for row vectorSeparate each data-point by semicolon for a column vectorShortcut when there is an addition pattern (colon)Shortcut when a specific amount of data points are linearly spaced (linspace())Realize that creating matrices only requires combining all of the above, while respecting one crucial rule:A matrix must remain rectangular at all times (i.e. no holes within the matrix)What does the apostrophe do?`
`Wrapping UpKnow by heart each way to create a row/column vector.Hard-code each data pointSeparate each data-point by comma or spaces for row vectorSeparate each data-point by semicolon for a column vectorShortcut when there is an addition pattern (colon)Shortcut when a specific amount of data points are linearly spaced (linspace())Realize that creating matrices only requires combining all of the above, while respecting one crucial rule:A matrix must remain rectangular at all times (i.e. no holes within the matrix)What does the apostrophe do?Restate some examples of vector operations and matrix operations.`
``` ```
``` ```
``` (adsbygoogle = window.adsbygoogle || []).push({}); Related Presentations More by User CS100J 10 April 2005 Rectangular arrays and ragged arrays. Secs. 9.1 9.3 Additional Data Types: 2-D Arrays, Logical Arrays, Strings -Selim aksoy bilkent university department of computer engineering [email protected] a = [ 1:2:7; 10:2:16 ] a = 1 3 5 7 10 12 14 16 [ x, y ] Control Arrays, Records, and Record Arrays in V.B. -. week 10. array example - sorting. in this example a simple set of inputs are set up. clicking the top button allows data entered to be stored in the array the middle one sorts the data the Creating Companies | Creating Wealth | Creating Jobs January 2013 -Aflasafe commercialization strategy improving the health and securing the income of nigeria’s small holder farmers. creating companies | creating wealth | creating jobs january Access 2010 Level 1 Unit 2 Creating Forms and Reports Chapter 6 Creating Reports and Mailing Labels -Access 2010 level 1 unit 2 creating forms and reports chapter 6 creating reports and mailing labels. creating reports and mailing Creating Sales by Creating Flow -By mountainside herbal nursery scholls, oregon. practice set up. my favorite market chair. one space set up. one space “u”. two space set up deep. two space set up shallow. two space double “u”. Arrays -Contents. objectives9.1 intro example: ms. white\'s test score analyzer9.2 arrays9.3 sorting9.4 searching9.5 processing command-line arguments9.6 multidimensional arrays9.7 graphics example: a piechart classpart of the picture: numerical Arrays -Today\'s lesson plan. discuss labs, quizzes, and tda topics (10 min)discussion posts: some excellent posts this week!class Arrays -1d array representation in java, c, and c . 1-dimensional array x = [a, b, c, d]map into contiguous memory locations. a. b. c. d. location(x[i]) = start i. space overhead. space overhead = 4 bytes for start 4 Arrays -Table of contents. declaring and creating arraysaccessing array elementsconsole input and output of arraysiterating over arrays using for and foreachmatrices and multidimensional arraysdynamic arrayslists<t>copying arrays. declaring and Arrays -6.1.1 the string class. 6.1.2 the masquerade and the + operator. 6.2 string handling. 6.2.1 ... create a string without using new: the masquerade. string s Arrays -The array data structure. an array is an indexed sequence of componentstypically, the array occupies sequential storage locationsthe length of the array is determined when the array is created, and cannot be changedeach component of the array Arrays -Overview. 7.1 introduction to arrays 7.2 arrays in functions7.3 programming with arrays7.4 multidimensional arrays. 7.1. introduction to arrays. introduction to arrays. an array is used to process a collection of data of the same Arrays -. suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade?65.281.731.395.4 < - highest grade76.158.6. individual data items. if we knew that there were always 150 students in the Arrays -As adts. an array is an abstract data typethe array type has a set of valuesthe values are all the possible arraysthe array type has a set of operations that can be applied uniformly to each of these valuesthe only operation is Arrays -? 2004 pearson addison-wesley. all rights reserved. 7-2. arrays. arrays are objects that help us organize large amounts of informationchapter 7 focuses on:array declaration and usebounds checking and capacityarrays that store object Arrays Arrays -? 2004 pearson addison-wesley. all rights reserved. 7-2. outline. declaring and using arraysarrays of objectstwo-dimensional arraysthe arraylist class. . ? 2004 pearson addison-wesley. all rights reserved. 7-3. arrays. an array is an ordered Arrays -Chapter 2. 3. structure array is objects: a set of pairs <index, value> where for each value of index there is a value from the set item. index is a finite ordered set of one or more dimensions, for example, Arrays -Array. declaring and allocating arrays int c __gc[] ;// declare a managed arrayc= new int __gc[12];// allocate space for arrayarrays are allocated with new because arrays are objects and all objects must be created with newmanaged types are Arrays -One-dimensional arrays . a list of values with the same data type that are stored using a single group name (array name).general array declaration Arrays 1 of 5 ```
``` ```
``` Download Presentation Connecting to Server.. English Français About Us | Advertise | Terms of Use | Privacy Policy | Contact Us | Blog © 2017 SlideServe. All rights reserved | Powered By DigitalOfficePro var subdom = window.location.href.split("/")[2].split(".")[0]; <!------- g analytics[ ------> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-31359012-1', 'auto'); ga('send', 'pageview'); <!------- g analytics ] ------> <!------- fb comment[ ------> (function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.6"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); <!------- fb comment] ------> //------------------------------------------------------------------------------------------- //on jquery load event function onjqueryload() { //------------------------------------------------------------------------------------------- //Load javascript files loadHandler.load("/new/script/bootstrap.min.js","script",null); loadHandler.load("/js/player.min.js","script",null); loadHandler.load("/js/winHandler.min.js","script",null); loadHandler.load("/js/viewPageHandler.min.js","script",null); //------------------------------------------------------------------------------------------- \$(document ).ready(function() { /*\$.post( "/track.php", {vid:3901722,uid:32935,vlink:"creating-arrays"}) .done(function( data ) {});*/ }); } //------------------------------------------------------------------------------------------- ```