Illustration of vectors of structures, MATLAB in Engineering

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');

Posted Date: 10/20/2012 6:04:27 AM | Location : United States







Related Discussions:- Illustration of vectors of structures, Assignment Help, Ask Question on Illustration of vectors of structures, Get Answer, Expert's Help, Illustration of vectors of structures Discussions

Write discussion on Illustration of vectors of structures
Your posts are moderated
Related Questions
Example Exit modular program: In the illustration below, the user  Chose the Limit; -   Whenever prompted for n, entered the two invalid values before finally ente

Intersect function and setdiff function: The intersect function rather than returns all the values which can be found in both of the input argument vectors. >> intersect(v

Binary Search: The binary search supposes that the vector has been sorted first. The algorithm is just similar to the way it works whenever looking for a name in a phone direc

Matrix definitions: As we know the matrix can be thought of as a table of values in which there are both rows and columns. The most common form of a matrix A (that is sometime

Program to Counting in a while loop: The script initializes variable counter to 0. Then, in the while loop action, each and every time the user successfully enter a number, 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 modular program: In a modular program, there would be one main script which calls three separate functions to complete these tasks: A function to prompt an us

Individual structure variable: The individual structure variable for one software package may look like this: The name of the structure variable is a package; it has f

Matrix operations: There are some common operations on matrices. The operators which are applied term by term, implying that the matrices should be of similar size, sometimes

Examine exponential function: The algorithm for the main script program is shown below:  Call a function eoption to show the menu and return the user's choice.  Loop