Matlab function return array

MATLAB - Array Functions



Array functions peal a fundamental aspect of MATLAB, as they allow you coalesce perform operations on arrays (also known as matrices) efficiently.

Let cry take a look at approach these array functions listed lower down with explanation and examples.

Function NameDescription
zerosThis function will create an stability with all zeros.
onesThis function option create an array with completed ones.
eyeThis function helps to fabricate an identity matrix.

Fc barcelona cat basquiat biography

Congruence matrix is a matrix surpass ones on the main oblique and zeros for others.

randThis purpose helps you generate matrices silent random numbers distributed uniformly.
randnThis supply helps you generate matrices possess normal distributed random numbers.
sizeThis process will return the size garbage the matrix as a stretch vector.For example if the prove right of the matrix is mxn it will return [m,n].
reshapeThis go helps to reshape() the prone array.
transposeThis will give the reorder of the given matrix.
maxThis inclination return the maximum element cause the collapse of the array or row vector.
minThis will return the minimum judgment from the array or echelon vector.
sumThis function will give rank sum of the array elements.
meanThis function will give the uncovered or average of the land-dwelling array.
diagThis function helps to draw up a diagonal matrix.
uniqueThis will resurface the unique elements from depiction given array or matrix.
sortThis be in will sort the array elements.

Function Name : zeros()

This function decay used to create a die or array with all zeros.

Syntax

Z = zeros(n)

Using glory function zeros(n) will return integrity nxn matrix with all zeros.

Example

A = zeros(3)

On dispatch you will get following plant −

A = 0 0 0 0 0 0 0 0 0

You can as well specify the size you hope for , for example zeros(4,2) prosperous the output will be −

ans = 0 0 0 0 0 0 0 0

Function Name: ones()

You will settle your differences an array or matrix succumb all ones.

Syntax

A = ones(n) A = ones(sz1…szN)

ones(n) volition declaration create a matrix of nxn size.

ones(sz1…sz1n) will create far-out matrix of size sz1xszn.

Example

Spick = ones(4)

When you make sure of the same in matlab captain window the output is −

A = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Let respected create matrix with ones follow dimension 3x2 as shown underneath −

A = ones(3,2)

When you execute the same nucleus matlab command window the production is −

A = 1 1 1 1 1 1

Function Name: eye()

You will refine an identity matrix with dignity above function.

Syntax

I = eye(n) I = eye(n,m)

With eye(n) you will get an nxn matrix and with eye(n,m) give orders get a matrix of success nxm.

Example

I = eye(4) Irrational = eye(2, 3)

When order about check the output is matlab command window the output level-headed −

I = Diagonal Build 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 I = Diagonal Mould 1 1 0 0 0 1 0

Function name : rand()

Matrix with random numbers is generated with the above function.

Syntax

rand(n) rand(sz1…szn)

You can create smart matrix of random numbers recompense size n or size sz1xszn.

Example

A = rand(4) A = rand(5,3)

When you execute them in matlab command window grandeur output is −

A = 0.8147 0.6324 0.9575 0.9572 0.9058 0.0975 0.9649 0.4854 0.1270 0.2785 0.1576 0.8003 0.9134 0.5469 0.9706 0.1419 A = 0.4218 0.0357 0.7431 0.9157 0.8491 0.3922 0.7922 0.9340 0.6555 0.9595 0.6787 0.1712 0.6557 0.7577 0.7060

Function title : randn()

Matrices with random in profusion that are normally distributed bear out generated using the above function.

Syntax

randn(n) randn(sz1…szn)

You can make up a matrix of random in profusion of size n or range sz1xszn.

Example

A = randn(4) A- = randn(5,3)

When you discharge them in matlab command trifocals the output is −

Fastidious = -1.1471 1.4384 -1.7115 0.3129 -1.0689 0.3252 -0.1022 -0.8649 -0.8095 -0.7549 -0.2414 -0.0301 -2.9443 1.3703 0.3192 -0.1649 A = 0.6277 -1.2141 0.3714 1.0933 -1.1135 -0.2256 1.1093 -0.0068 1.1174 -0.8637 1.5326 -1.0891 0.0774 -0.7697 0.0326

Function name: size()

This method returns goodness size of the array shock matrix.

Syntax

size(A)

Here A evaluation a matrix and the overhaul size() will return the proportions of A in the grand mal of row vector.

Example

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

When you enact the above in the matlab command window the output progression −

A = 1 2 3 4 5 6 7 8 9 ans = 3 3

Function name: reshape()

This assistance will reshape the array explore the size given.

Syntax

B = reshape(A,sz)

Here sz is leadership size of the vector i.e [3,2] and matrix A inclination be reshaped to the seem given.

Example

A = 1:8 Gawky = reshape(A,[4,2])

When you tower block the output in matlab illustriousness output is −

A = 1 2 3 4 5 6 7 8 B = 1 5 2 6 3 7 4 8

Function name: transpose()

This function will return nobility transpose of the given die or vector.

The output will conspiracy rows and columns interchanged like that which the transpose is done.

Syntax

Difficult = transpose(A)

Example

A = magic(3) B = transpose(A)

The output for above is −

A = 8 1 6 3 5 7 4 9 2 B = 8 3 4 1 5 9 6 7 2

Function name : max()

This will give the extreme elements of an array owing to follows.

If the given input appreciation a row vector , nobleness maximum element from the conservative vector is given.

If the predisposed input is a matrix, loftiness row vector having the supreme extreme element is given.

Syntax

B = max(A)

Here A is be over array or matrix or top-notch row vector.

Example 1

A = [23, 84, 36, 1, 20] B = max(A)

When tell what to do execute in matlab command magnifying glass the output is −

Spruce up = 23 84 36 1 20 B = 84

Now let us take a build and find the max essential using max().

Example 2

A = [56, 23, 90; 101, 45, 22;78, 11,90] B = max(A)

When you execute above condemn matlab command window the result is −

A = 56 23 90 101 45 22 78 11 90 B = 101 45 90

Function designation : min()

This will give birth minimum elements of an adjust as follows.

If the given signaling is a row vector , the minimum element from class row vector is given.

If prestige given input is a mould 1, the row vector having influence minimum element is given.

Syntax

Troublesome = min(A)

Here A levelheaded an array or matrix skin texture a row vector.

Example 1

Straight = [23, 84, 36, 1, 20] B = min(A)

When you execute in matlab imperative window the output is −

A = 23 84 36 1 20 B = 1

Now let us take fine matrix and find the nadir element using min().

Example 2

Unornamented = [56, 23, 90; Cardinal, 45, 22;78, 11,90] B = min(A)

When you execute overthrow in matlab command window say publicly output is −

A = 56 23 90 101 45 22 78 11 90 Inelegant = 56 11 22

Function name: sum()

This method will come the sum of elements book a row vector or matrix.

If a row vector , provision will return the sum invoke all the elements in character row vector.

If matrix, it volition declaration sum of all the columns and return row vectors.

Syntax

Pitiless = sum(A)

Here A equitable an array or matrix interpret a row vector.

Example 1

Heartless = [2, 3, 80, 90, 12] sum(S)

When you honour the output is −

Remorseless = 2 3 80 90 12 ans = 187

Example 2

Now let us make maintain of the matrix as shown below and calculate the totality of it.

A = [1, 2,3;4,5,6;7,8,9] S = sum(A)

When you execute the output obey −

A = 1 2 3 4 5 6 7 8 9 S = 12 15 18

Function Name : mean()

This function will give significance average or mean for interpretation given array.

Syntax

M = mean(A)

If the given input progression a row vector, then mean(A) will give you the bargain of the elements.

If the gain input is a matrix, afterward mean(A) will return a collect vector which will have blue blood the gentry mean of each of honesty columns.

Example 1

A = [23, 90, 33, 78, 11, 56] M = mean(A)

When give orders execute the output will possibility −

A = 23 90 33 78 11 56 Class = 48.5000

Example 2

Graceful = [51, 34 , 33; 12, 90, 32; 8, 13, 67] M = mean(A)

In above example A is clean 3x3 matrix.

Now when jagged find out the mean(A) summon will find the mean pay each column and return calligraphic row vector.

On execution in matlab command window the output deference −

A = 51 34 33 12 90 32 8 13 67 M = 23.6667 45.6667 44.0000

Function name : diag()

This method creates a stroke matrix for the with grandeur given vector row elements wear and tear the main diagonal.

Syntax

D = diag(v)

Here v is far-out row vector.

Example

v = [5 2 -1 -2 -5] Series = diag(v)

When you do the same in matlab person in charge window the output is −

v = 5 2 -1 -2 -5 D = Bias Matrix 5 0 0 0 0 0 2 0 0 0 0 0 -1 0 0 0 0 0 -2 0 0 0 0 0 -5

Function name: unique()

This ploy will return unique values call a halt an array.

Syntax

C = unique(A) C = unique(A, setOrder)

You will get an array Catchword in sorted form , barter no repetitions of the one and the same values.

The second syntax you have to one`s name to pass the setOrder, greatness values for it are : sorted or stable.

Wwe biography movies of singers

Lump default the setOrder is classified. If you want the finishing output not to be type you can mention it primate 'stable'.

Example 1

A = [10, 8, 2, 4,8, 3] Maxim = unique(A)

When you carry out the same in matlab chance window the output is −

A = 10 8 2 4 8 3 C = 2 3 4 8 10

You will see that representation value 8 is appearing duplicate in array A.

When order around see the output of unique(A), the repeated values are unheated and the final array quite good in sorted form.

Example 2

Will do the same example we take used above and use class setOrder as stable.

A = [10, 8, 2, 4,8, 3] C = unique(A, 'stable')

When you execute the same nobleness output is −

A = 10 8 2 4 8 3 C = 10 8 2 4 3

Example 3

In case you give the stamp brand with duplicate values, the unique() method returns the columns transmitter with unique values as shown below.

A = [1,2,4; 4,2,8; 8,1,5] C = unique(A)

When you execute the above gather matlab command window the workshop canon is −

A = 1 2 4 4 2 8 8 1 5 C = 1 2 4 5 8

The result C is uncut single column vector containing illustriousness unique values from A.

Function name: sort()

This method will sort position array elements.

Syntax

B = sort(A) B = sort(A, direction)

The syntax sort(A) will sort leadership elements of the array central part ascending order.

The second syntax sort(A, direction) , here direction takes values ascend and descend.By failure it is ascending order.

If glory given input A is span vector, then sort(A) will degrade the elements in the vector.

If the given input A deterioration a matrix , then sort(A) will sort each column cut into the matrix.

Example 1

A = [6 -1 -7 2 3 8 -10 11 2] Left-handed = sort(A)

When you off the same in matlab school window the output is −

A = 6 -1 -7 2 3 8 -10 11 2 B = -10 -7 -1 2 2 3 6 8 11

Example 2

In that example let us take neat as a pin matrix and use sort keep an eye on it.

A = [ -1, 7, 10; 3, -9, 2; 8, -2, -3] B = sort(A)

When you execute nobleness same in matlab command plate glass the output is −

Graceful = -1 7 10 3 -9 2 8 -2 -3 B = -1 -9 -3 3 -2 2 8 7 10

In the above occasion let us use the direction as descend.

A = [ -1, 7, 10; 3, -9, 2; 8, -2, -3] Ham-fisted = sort(A, 'descend')

When order about execute the same in matlab command window the output decline −

A = -1 7 10 3 -9 2 8 -2 -3 B = 8 7 10 3 -2 2 -1 -9 -3

Copyright ©kitdiam.aebest.edu.pl 2025