Nothing Special   »   [go: up one dir, main page]

Multidimensional Arrays:: 1. Explain About Multidimensional Array? (Model Question)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

1. Explain about multidimensional array?

(Model question)
Multidimensional Arrays:
An array having more than two dimensions is called a multidimensional
array in MATLAB. Multidimensional arrays in MATLAB are an extension of
the normal two-dimensional matrix.
Generally to generate a multidimensional array, we first create a two-
dimensional array and extend it.
For example, let's create a two-dimensional array a.

a = [7 9 5; 6 1 9; 4 3 2]

MATLAB will execute the above statement and return the following result

a=
7 9 5
6 1 9
4 3 2

The array a is a 3-by-3 array; we can add a third dimension to a, by


providing the values like −

a(:, :, 2)= [ 1 2 3; 4 5 6; 7 8 9]

MATLAB will execute the above statement and return the following result

a=
ans(:,:,1) =
0 0 0
0 0 0
1
0 0 0
ans(:,:,2) =
1 2 3
4 5 6
7 8 9

We can also create multidimensional arrays using the ones(), zeros() or


the rand() functions.
For example,

b = rand(4,3,2)

MATLAB will execute the above statement and return the following result

b(:,:,1) =
0.0344 0.7952 0.6463
0.4387 0.1869 0.7094
0.3816 0.4898 0.7547
0.7655 0.4456 0.2760

b(:,:,2) =
0.6797 0.4984 0.2238
0.6551 0.9597 0.7513
0.1626 0.3404 0.2551
0.1190 0.5853 0.5060

We can also use the cat() function to build multidimensional arrays. It


concatenates a list of arrays along a specified dimension −
2
Syntax for the cat() function is −

B = cat(dim, A1, A2...)

Where,
 B is the new array created
 A1, A2, ... are the arrays to be concatenated
 dim is the dimension along which to concatenate the arrays
Example
Create a script file and type the following code into it −

a = [9 8 7; 6 5 4; 3 2 1];
b = [1 2 3; 4 5 6; 7 8 9];
c = cat(3, a, b, [ 2 3 1; 4 7 8; 3 9 0])

When you run the file, it displays −

c(:,:,1) =
9 8 7
6 5 4
3 2 1
c(:,:,2) =
1 2 3
4 5 6
7 8 9
c(:,:,3) =
2 3 1
4 7 8

3
2. Explain about arrays & Element by Element Operations of arrays?
(Model question)
Ans: Arrays: MATLAB variables are arrays of numbers. An array consisting
of one element is called a scalar. For example; x =2
x has the value 2. More commonly, x will assume a number of values,
say from 0 to 5, For example; x = 1:0.5:5 Now x is an array of numbers;
x = [0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]
Note,
1:0.5:5 creates an array of numbers from 1 to five in steps of 0.5.
Examples of arrays
A = [1, 2; 3, 4; 5, 6]
Creates a 3x2 array, 3 rows and 2 columns. The semicolon creates a new
row.
A= 1 2
3 4
5 6
x = [4; 5]
Creates an array with 2 rows and one column.
x=4
5
If we multiply A by x, the rules of matrix algebra apply. That is,
A*x = 1*4 + 2*5 = 14
3*4 + 4*5 32
5*4 + 6*5 50
A*x is an array with 3 rows and one column.

4
Element by Element Operations:
Many times matrix multiplication is not desired. For example, when
we want the square in each element in an array.
x = 0:1:4
y = x .* x
Note that .* is used to denote element by element operation. The result is;
x= [0 1 2 3 4]
y = [0 1 4 9 16]
Also If stem replaces plot in the above examples, a plot of discrete values
represented by a lollipop structure results. For example;
stem(x,y)You can add axis labels and a title to stem plots.
3. Explain about Polynomial Operations Using Arrays in mat lab?
Ans: (Model question)
Polynomial Operations Using Arrays:
MATLAB has some convenient vector-based tools for working with
polynomials, which are used in many advanced courses and applications in
engineering. Type help polyfun for more information on this category of
commands. We will use the following notation to describe a polynomial:
f(x) =alxn +a2Xn-1 + a3Xn-2 + … +an_Ix2 +anx + an+1
This polynomial is a function of x. Its degree or order is n, the highest
power of x that appears in the polynomial. The a., i = 1,2, … , n + 1 are the
polynomial’s coefficients. We can describe a polynomial in MATLAB with. a
row vector whose elements are the polynomial’s coefficients, starting with
the coefficient of the
highest power of x. This vector is [al. a2, a3, …• an-I, an, an+d.

5
For example, the vector [4, – 8, 7 , – 5) represents the polynomial ax? –
8×2 +7x – 5. Polynomial roots can be found with the root s (a) function,
where (a) is the array containing the polynomial coefficients. For example,
to obtain the roots of x3 + 12×2 +45x +50 = 0, you type y = root s ( [1, 12,
45, 50) ) . The answer (y) is a column array containing the values -2, -5, -5.
The poly (r) function computes the coefficients of the polynomial
whose roots are specified by the array r. The result is a row array that
contains the polynomial’s coefficients. (Note that the root s function
returns it column array.) For example, to find the polynomial whose roots
are I and 3 ± 5i, the session is
»r=[l,3+5i,3-5i);
»poly(r)
ans =
1 -7 40 -34
Thus the polynomial is x3 – 7×2 + 40x – 34. The two commands could
have been combined into the single command poly ( [1, 3+ 5i, 3 – 5 i) ) .
Polynomial Addition and Subtraction
To add two polynomials, add the arrays that describe their coefficients. If
the polynomials are of different degrees, add zeros to the coefficient array
of the lower-degree polynomial. For example, consider
f(x)=9×3–5×2+3x+7
whose coefficient array is
f=[9,–5,3,7] and
g(x)=6×2–X+2
whose coefficient array is g = [6, -1 , 2 ]. The degree of g(x) is one less that

6
of f(x). Therefore, to add f(x) and g(x), we append one zero to g to “fool”
MATLAB into thinking g(x) is a third-degree polynomial. That is, we
type g = [0 g] to obtain [0, 6 , -1, 2] for g. This vector represents g(x) = Ox3
+ 6×2 – X + 2. To add the polynomials, type h = f+g. The result is h
= [9,1,2,9], which corresponds to h(x) = 9×3 + x2 + 2x + 9. Subtraction is
done in a similar way:
Polynomial Multiplication and Division
To multiply a polynomial by a scalar, simply multiply the coefficient array
by that ‘scalar. For example, 5th (x) is represented by [45, 5 , 10 , 45
J. . Multiplication of polynomials by hand can be tedious, and polynomial
division
is even more so, but these operations are easily done with MATLAB.
Use the cony function (it stands for “convolve”) to multiply polynomials
and use the deconv function (deconv stands for “deconvolve”) to perform
synthetic division. Table 2.5-1 summarizes these functions, as well as the
poly, polyval,and roots functions, which we saw The product of the
polynomials f(x) and g(x) is
f(x)g(x) = (9×3–5×2+3x+ 7)(6×2–X+2)–=54×5–39×4+41×3+29×2 –X+14
Dividing f(x) by g(x) using synthetic division gives a quotient of
f(x) 9×3 – 5×2 +3x+7.=1.5x–0.5833
g(x)=6×2–X+2
with a remainder of -0.5833x +8.1667. Here is the MATLAB session to
perform these operations.
»f=[9,-5,3,7];

7
»g=[6,-I,2];
»product = conv(f,g)
product’=54 -39 41 29
»[quotient,.remainder]=
quotient= 1.5-0.5833
remainder= o0-114
deconv(f,g)
-0.5833 8.1667
The conv and deconv- functions do not require that the polynomials have
the same degree, so we did not have to fool MATLAB as we did when
adding the polynomials. Table 25-1 gives the general syntax for the cony
and deconv functions.
PlottingPolynomials
The polyval (a, x’) function evaluates a polynomial at specified values of
its independent variable x, which can be a matrix or a vector. The
polynomial’s coefficient array is a. The result is the same size as x. For
example, to evaluate the polynomial f(x) = 9×3 – 5×2 + 3x + 7 at the points
x=0,2,4,…,10,
type
»a=[9,–5,3,7] ;
»x[0:2:10);
»fpolyval(a,x);
The resulting vector f contains six values that correspond to /(0). /(2), /(4) /
(10). These three commands can be combined into a single command:

8
»f=polyval([9,-5,3,7),[0:2:10));
Personal preference determines whether to combine terms in this way;
some people think that the single. combined command is less readable
than three separate commands.
The poly val function is very useful for plotting polynomials. To do this
you should define an array that contains many values of the independent
variable x in order to obtain a smooth plot. For example, to plot the
polynomial f(x)= 9×3–5×2+3x+7for -2 :s x :s 5, you type
»a[9,-5,3,7);
»x[-2:0.01:5);
»f polyval (a,x); »plot(x,f),xlabel (‘x’),ylabe1(‘f(x) ‘),grid
4. Write about cell arrays in mat lab? (Model question)
Ans: Cell Array
Cell arrays are arrays of indexed cells where each cell can store an array of
a different dimensions and data types.
The cell function is used for creating a cell array. Syntax for the cell
function is −

C = cell(dim)
C = cell(dim1,...,dimN)
D = cell(obj)

Where,
 C is the cell array;
 dim is a scalar integer or vector of integers that specifies the
dimensions of cell array C;
 dim1, ... , dimN are scalar integers that specify the dimensions of C;
9
 obj is One of the following:
o Java array or object
o .NET array of type System.String or System.Object
Example
Create a script file and type the following code into it −

c = cell(2, 5);
c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5}

When you run the file, it displays the following result −

c=
{
[1,1] = Red
[2,1] = 1
[1,2] = Blue
[2,2] = 2
[1,3] = Green
[2,3] = 3
[1,4] = Yellow
[2,4] = 4
[1,5] = White
[2,5] = 5
}

5. Explain about structure array in mat lab? (Model question)


Ans: A structure array is a data type that groups related data using data

10
containers called fields. Each field can contain any type of data. Access
data in a field using dot notation of the form structName.fieldName.
Creation
When you have data to put into a new structure, create the structure using
dot notation to name its fields one at a time:
s.a = 1;
s.b = {'A','B','C'}
s = struct with fields:
a: 1
b: {'A' 'B' 'C'}

You also can create a structure array using the struct function, described
below. You can specify many fields simultaneously, or create a nonscalar
structure array.
Syntax
s = struct
s = struct(field,value)
s = struct(field1,value1,...,fieldN,valueN)
s = struct([])
s = struct(obj)
Description
s = struct creates a scalar (1-by-1) structure with no fields.
example s = struct(field,value) creates a structure array with the specified
field and values. The value input argument can be any data type, such as a
numeric, logical, character, or cell array.

11
 If value is not a cell array, or if value is a scalar cell array, then s is a
scalar structure. For instance, s = struct('a',[1 2 3]) creates a 1-by-1
structure, where s.a = [1 2 3].
 If value is a nonscalar cell array, then s is a structure array with the
same dimensions as value. Each element of scontains the corresponding
element of value. For example,s = struct('x',{'a','b'},'y','c') returns s(1).x =
'a', s(2).x = 'b', s(1).y = 'c', and s(2).y = 'c'.
 If value is an empty cell array {}, then s is an empty (0-by-0) structure.
example
s = struct(field1,value1,...,fieldN,valueN) creates multiple fields. Any
nonscalar cell arrays in the setvalue1,...,valueN must have the same
dimensions.
 If none of the value inputs are cell arrays, or if all valueinputs that are
cell arrays are scalars, then s is a scalar structure.
 If any of the value inputs is a nonscalar cell array, then shas the same
dimensions as the nonscalar cell array. For any value that is a scalar cell
array or an array of any other data type, struct inserts the contents
of value in the relevant field for all elements of s.
 If any value input is an empty cell array, {}, then output sis an empty
(0-by-0) structure. To specify an empty field and keep the values of the
other fields, use [] as a value input instead.
s = struct([]) creates an empty (0-by-0) structure with no fields.
s = struct(obj) creates a scalar structure with field names and values that
correspond to properties of obj. The struct function does not convert obj,
but rather creates s as a new structure.
12

You might also like