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
Creating the structure Variables: Creating a structure variable can be accomplished by simply storing the values in fields by using assignment statements, or by using the stru

Vector operations: As vectors are special cases of matrices, the matrix operations elaborated (addition, subtraction, multiplication, scalar multiplication, transpose) work on

Interpolation and extrapolation: In most cases, it is desired to estimate values other than at the sampled data points. For illustration, we may want to estimate what the temp

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

Anonymous Functions: The anonymous function is a very easy, one-line function. The benefit of an anonymous function is that it does not have to be stored in an M-file. This ca

How can I use the weighted moving average formula in matlab to smooth a column data of 404 values?

Executing a program: Running the program would be completed by typing the name of the script; this would call the other functions: >> calcandprintarea Whenever prompt

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

Finding a sting - function findstr: The function findstr receives two strings as input arguments. It finds all the occurrences of shorter string contained by the longer, and r

Logical scalar values: The MATLAB also has or and and operators which work element wise for the matrices: These operators will compare any of the two vectors or matric