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
Graphics Properties: The MATLAB uses the Handle Graphics in all its figures. All figures consist of various objects, each of which is assigned a handle. The object handle is a

Use of Nested if-else statements: By using the nested if-else to select from among the three possibilities, not all the conditions should be tested. In this situation, if x is

Illustration of Gauss elimination: For illustration, for a 2 × 2 system, an augmented matrix be: Then, the EROs is applied to obtain the augmented matrix into an upper

Subfunctions: Though, it is possible to have more than one function in a given M-file. For illustration, if one function calls the other, the first function would be the prima

Q.1: Consider the transmission of a sinusoid x(t) = cos(2f0t) through a channel a ected by multipath and Doppler. Let there be two paths, and assume the sinusoid is being sent fro

Gauss Elimination: The Gauss elimination technique consists of:    Generating the augmented matrix [A b]    Applying EROs to augmented matrix to obtain an upper trian


1. Write a MATLAB function (upperTriangle) using the functions you previously created to convert a matrix to upper triangular form. Start with row 1, column1. Find the row that has

Illustration of Variable scope: Running this function does not add any of variables to the workspace, as elaborated: >> clear >> who >> disp(mysum([5 9 1]))

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 =