Illustration of vectors of structures, MATLAB in Engineering

Assignment Help:

Illustration of Vectors of structures:

In this illustration, the packages are vector which has three elements. It is shown as a column vector. Each and every element is a structure consisting of four fields, item_no, price, cost, and code. It may look like a matrix with rows and columns, but it is rather a vector of structures.

This can be generated in several ways. The one method is to generate a structure variable, as shown, to store the information on one software package. This can then be extended to be the vector of structures.

>> packages = struct('item_no',123,'cost',19.99,.  .  .

'price',39.95,'code','g');

>> packages(2) = struct('item_no',456,'cost', 5.99,.  .  .

'price',49.99,'code','l');

>> packages(3) = struct('item_no',587,'cost',11.11,.  .  .

'price',33.33,'code','w');

The first assignment statement shown generates the first structure in the structure vector; the next one generates the second structure, and so on. This really generates a 1 × 3 row vector.

Alternatively, the first structure could be considered as a vector to start with, taking illustration,

 

>> packages(1) = struct('item_no',123,'cost',19.99,.  .  .

'price',39.95,'code','g');

>> packages(2) = struct('item_no',456,'cost', 5.99,.  .  .

'price',49.99,'code','l');

>> packages(3) = struct('item_no',587,'cost',11.11,.  .  .

'price',33.33,'code','w');

 

Both of these techniques, though, include extending the vector. As we know that, preallocating any vector in MATLAB is more efficient than extending it. There are various techniques of preallocating the vector. By beginning with last element, the MATLAB would generate a vector with many elements. Then, the elements from 1 throughout end-1 could be initialized. For illustration, for a vector of structures which has three elements, begin with the third element.

>> packages(3) = struct('item_no',587,'cost',11.11,.  .  .

'price',33.33,'code','w');

>> packages(1) = struct('item_no',123,'cost',19.99,.  .  .

'price',39.95,'code','g');

>> packages(2) = struct('item_no',456,'cost', 5.99,.  .  .

'price',49.99,'code','l');


Related Discussions:- Illustration of vectors of structures

Printrectarea function - subfunction, printrectarea function: functio...

printrectarea function: function call: printrectarea(length, width) function header: function printrectarea(len, wid)   In the function call, there are two argume

Illustration of image processing, Illustration of Image processing: Th...

Illustration of Image processing: This displays that there are 64 rows, or in another word, 64 colors, in this specific colormap. It also displays that the first five colors a

Gauss-jordan, Gauss-Jordan: The Gauss-Jordan elimination technique beg...

Gauss-Jordan: The Gauss-Jordan elimination technique begins in similar way which the Gauss elimination technique does, but then rather than of back-substitution, the eliminati

Ischar function - string, ischar function: The ischar function return ...

ischar function: The ischar function return the logical true if an array is a character array, or logical false if not. >> vec = 'EK127'; >> ischar(vec) ans =

Text graphic function - graphics objects, Text graphic function - Graphics ...

Text graphic function - Graphics objects: The text graphic function permits text to be printed in a Figure Window, involving special characters which are printed by using \spe

Built-in colormaps - image processing, Built-in colormaps: The MATLAB ...

Built-in colormaps: The MATLAB has numerous built-in colormaps which are named; the reference page on colormap shows them. Calling the function colormap without passing any ar

Illustration of finding a sting, Illustration of finding a sting: Le...

Illustration of finding a sting: Let's enlarge this, and write a script which creates a vector of strings which are phrases. The outcome is not suppressed so that the string

Reading from a file in a while loop, Reading from a File in a While Loop: ...

Reading from a File in a While Loop: Though in most languages the combination of a loop and an if statement would be essential to determine whether or not the elements in a ve

Example of gauss-jordan, Example of Gauss-jordan: For a 2×2 system, th...

Example of Gauss-jordan: For a 2×2 system, this would results and for a 3 × 3 system, Note that the resulting diagonal form does not involve the right-most col

Example of menu driven modular program, Example of Menu driven modular prog...

Example of Menu driven modular program: As an illustration of such a menu-driven program, we will write a program to discover the constant e. The constant e, known as the n

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd