วันพุธที่ 27 พฤษภาคม พ.ศ. 2563

โครงสร้างข้อมูล ติวเตอร์เรียล 16

Tutorial 16 – Data Structures

Scilab has support for data structures, such as cell, hypermatrices, list, mlist, rlist, tlist and struct. This tutorial will touch upon some of them, namely, list, cell and struct. List The list data structure allows operations such as insertion, deletion, concatenation. -->lst=list(1, [1, 2; 3, 4], [“a”, “b”, “c”]); // create a list -->lst(1) // extract first element of lst ans = 1. -->lst(2) ans = 1. 2. 2. 4. -->lst(3) ans = !a b c! -->size(lst) ans = 3. -->lst($) // extract last element of lst ans = !a b c! -->lst($+1) = “Append to list”; // append to lst -->lst($) ans = Append to list -->lst(0) = “Insert before list”; list(1) ans = Insert before list Cell The cell data structure allows the creation of matrices in which each element can be of different type. Normally, all elements of a matrix must be of the same data type. Thus a matrix cannot have one element as a number while another element is a string. The cell data structure does not have this limitation. In fact, each element in turn can be another data structure, thereby allowing construction of complex data structures. -->a = cell(3) // create a cell with 3 rows and 3 columns a = !{} {} {} ! ! ! Tutorial 16 – Data Structures | 43 !{} {} {} ! ! ! !{} {} {} ! -->b = cell(3,1); // create a cell with 3 rows and 1 column -->c = cell(2, 3, 4); // create a cell with 2 rows, 3 columns and 4 cards -->b(1).entries = 1:3; -->b(2).entries = “Scilab”; -->b(3).entries = [1 2; 3 4]; -->b b = ![1 2 3] ! ! ! !”Scilab” ! ! ! !{2x2 constant} ! -->b(1) ans = [1, 2, 3] -->b(2) ans = “Scilab” -->b(3) ans = ans = {2x2 constant) -->b(3).entries ans = 1. 2. 3. 4. -->b.dims ans = 3 1 -->size(b) ans = 3. 1. Struct The struct data structure allows the creation of user defined data types with user defined field names. The struct is analogous to the struct in C programming language, but is more flexible in that the new fields can be created even after the struct has been created. Each field can store any type of Scilab data, including matrices, cells, lists or even other structs. -->dt = struct('date', 15, 'month', 'Aug', 'year', 1947) dt = day: 15 month: “Aug” year: 1947 Tutorial 16 – Data Structures | 44 The above data structure contains three fields, namely, day, month and year. While day and year store numeric data, month stores string data. We can access the individual fields with the . (dot) operator. -->dt.day ans = 15. -->dt.month ans = Aug -->dt.year ans = 1947 We can create new fields by simply assigning data to a new filed name. -->dt.desc = “Independence day” dt = day: 15 month: “Aug” year: 1947 desc: “Independence day” A struct need not be explicitly created using the struct() function and a field can also contain matrices. -->s.type = “Space”; -->s.unit.length = “m”; -->s.unit.force = “kN”; -->s.nodes = [1 0 0 0; 2 0 5 0; 3 8 5 0; 4 8 0 0]; -->s s = type: “Space unit: [1x1 struct] nodes: [4x4 constant] -->s.unit ans = length: “m” force: “kN” -->s.nodes ans = 1. 0. 0. 0. 2. 0. 5. 0. 3. 8. 5. 0. 4. 8. 0. 0. Data structures help organize data in a hierarchical manner and simplify passing of data from one function to another. It now becomes possible to pack data into a single object and unpack the object to extract individual fields. Thus passing a single object as the argument to a function gives access to all the individual fields within the object inside it. Previously, we would have to pass each field of the data structures as an individual argument. Tutorial 16 – Data Structures | 45 As an illustration, let the different attributes representing a structure be (i) type of structure (possible values are space, plane, truss, grid etc.), (ii) node coordinates (a matrix with 3 columns), (iii) member connectivity (matrix with 3 columns) and (iv) material (a matrix with 3 columns). To pass the information about a structure to a function, we will have to use four arguments. Instead, if we define a data structure with the four attributes as its fields, we will need to pass only one argument to the function, and within the function, we can access the individual fields

ไม่มีความคิดเห็น:

แสดงความคิดเห็น