Skip to content Skip to sidebar Skip to footer

How to Read in Xls Data to a Cell Array Matlab

MATLAB - Arrays


All variables of all data types in MATLAB are multidimensional arrays. A vector is a ane-dimensional array and a matrix is a two-dimensional array.

We have already discussed vectors and matrices. In this chapter, we volition discuss multidimensional arrays. However, before that, let us discuss some special types of arrays.

Special Arrays in MATLAB

In this section, we volition discuss some functions that create some special arrays. For all these functions, a single statement creates a square array, double arguments create rectangular assortment.

The zeros() function creates an array of all zeros −

For instance −

zeros(5)        

MATLAB volition execute the above statement and return the following result −

ans =       0     0     0     0     0       0     0     0     0     0       0     0     0     0     0       0     0     0     0     0       0     0     0     0     0        

The ones() role creates an array of all ones −

For example −

ones(4,3)        

MATLAB will execute the to a higher place argument and return the post-obit result −

ans =       1     1     1       one     1     1       1     ane     ane       1     i     ane        

The eye() office creates an identity matrix.

For example −

eye(4)        

MATLAB will execute the above statement and return the following result −

ans =       1     0     0     0       0     1     0     0       0     0     1     0       0     0     0     i        

The rand() office creates an array of uniformly distributed random numbers on (0,i) −

For instance −

rand(iii, v)        

MATLAB volition execute the above statement and return the following issue −

ans =    0.8147    0.9134    0.2785    0.9649    0.9572    0.9058    0.6324    0.5469    0.1576    0.4854    0.1270    0.0975    0.9575    0.9706    0.8003        

A Magic Square

A magic square is a foursquare that produces the same sum, when its elements are added row-wise, column-wise or diagonally.

The magic() function creates a magic square array. Information technology takes a singular statement that gives the size of the foursquare. The argument must be a scalar greater than or equal to 3.

magic(4)        

MATLAB will execute the above statement and render the following result −

ans =    16     2     3    xiii    5    xi    10     8    ix     7     6    12    4    14    15     1        

Multidimensional Arrays

An array having more than two dimensions is chosen a multidimensional array in MATLAB. Multidimensional arrays in MATLAB are an extension of the normal two-dimensional matrix.

Mostly to generate a multidimensional array, we first create a ii-dimensional assortment and extend it.

For instance, let's create a two-dimensional array a.

a = [7 9 5; vi one ix; 4 3 ii]        

MATLAB will execute the above statement and return the following consequence −

a =    seven     9     5    half-dozen     1     9    four     3     ii        

The array a is a 3-by-three array; we tin add a third dimension to a, past providing the values like −

a(:, :, ii)= [ 1 two three; 4 5 6; 7 8 9]        

MATLAB will execute the above statement and return the following event −

a =  ans(:,:,i) =     0   0   0    0   0   0    0   0   0  ans(:,:,ii) =     1   2   three    four   5   6    7   8   9        

Nosotros can too create multidimensional arrays using the ones(), zeros() or the rand() functions.

For case,

b = rand(four,3,2)        

MATLAB will execute the above statement and return the following issue −

b(:,:,ane) =    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(:,:,two) =    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 besides use the cat() part to build multidimensional arrays. It concatenates a list of arrays along a specified dimension −

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 forth which to concatenate the arrays

Instance

Create a script file and type the following code into it −

a = [ix 8 7; 6 5 4; 3 2 1]; b = [i ii 3; 4 five half dozen; 7 8 9]; c = cat(3, a, b, [ 2 3 1; four 7 eight; 3 9 0])        

When you lot run the file, it displays −

c(:,:,1) =       9     eight     7       vi     five     4       iii     2     1 c(:,:,2) =       one     2     3       4     5     6       7     8     9 c(:,:,3) =       2     3     1       four     vii     viii       3     9     0        

Assortment Functions

MATLAB provides the following functions to sort, rotate, permute, reshape, or shift array contents.

Function Purpose
length Length of vector or largest array dimension
ndims Number of array dimensions
numel Number of array elements
size Array dimensions
iscolumn Determines whether input is column vector
isempty Determines whether assortment is empty
ismatrix Determines whether input is matrix
isrow Determines whether input is row vector
isscalar Determines whether input is scalar
isvector Determines whether input is vector
blkdiag Constructs cake diagonal matrix from input arguments
circshift Shifts assortment circularly
ctranspose Complex conjugate transpose
diag Diagonal matrices and diagonals of matrix
flipdim Flips assortment along specified dimension
fliplr Flips matrix from left to right
flipud Flips matrix upwards to down
ipermute Inverses permute dimensions of N-D array
permute Rearranges dimensions of Northward-D array
repmat Replicates and tile array
reshape Reshapes array
rot90 Rotates matrix 90 degrees
shiftdim Shifts dimensions
issorted Determines whether set up elements are in sorted order
sort Sorts array elements in ascending or descending lodge
sortrows Sorts rows in ascending social club
clasp Removes singleton dimensions
transpose Transpose
vectorize Vectorizes expression

Examples

The following examples illustrate some of the functions mentioned above.

Length, Dimension and Number of elements −

Create a script file and type the following code into it −

10 = [seven.one, 3.four, 7.two, 28/4, iii.6, 17, nine.4, 8.9]; length(ten)      % length of x vector y = rand(3, 4, 5, 2); ndims(y)       % no of dimensions in assortment y due south = ['Zara', 'Nuha', 'Shamim', 'Riz', 'Shadab']; numel(s)       % no of elements in s        

When y'all run the file, information technology displays the following event −

ans =  8 ans =  4 ans =  23        

Circular Shifting of the Array Elements −

Create a script file and type the post-obit code into it −

a = [ane 2 3; 4 v 6; 7 8 9]  % the original array a b = circshift(a,1)         %  circular shift showtime dimension values downwardly by ane. c = circshift(a,[i -1])    % circular shift first dimension values % down by i                             % and second dimension values to the left % by one.        

When you lot run the file, information technology displays the following result −

a =    1     2     3    four     v     half dozen    7     8     nine  b =    7     8     9    i     two     3    4     five     6  c =    8     ix     7    2     iii     1    5     6     4        

Sorting Arrays

Create a script file and type the following code into it −

v = [ 23 45 12 9 5 0 19 17]  % horizontal vector sort(v)                      % sorting 5 m = [2 half-dozen iv; 5 3 9; 2 0 i]    % two dimensional array sort(m, 1)                   % sorting m along the row sort(m, two)                   % sorting m along the column        

When you run the file, information technology displays the following consequence −

v =    23    45    12     9     5     0    19    17 ans =    0     five     9    12    17    19    23    45 grand =    2     6     4    5     3     ix    2     0     1 ans =    2     0     1    two     3     iv    5     6     9 ans =    ii     iv     6    3     5     ix    0     i     2        

Jail cell Assortment

Cell arrays are arrays of indexed cells where each jail cell tin store an array of a different dimensions and data types.

The jail cell function is used for creating a prison cell array. Syntax for the cell function is −

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

Where,

  • C is the prison cell assortment;

  • 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;

  • obj is One of the following −

    • Java array or object
    • .Cyberspace array of blazon Organisation.Cord or System.Object

Case

Create a script file and type the post-obit code into information technology −

c = cell(2, v); c = {'Scarlet', 'Blue', 'Dark-green', 'Yellowish', 'White'; 1 2 3 4 five}        

When you run the file, information technology displays the following effect −

c =  {    [ane,1] = Red    [two,ane] =  one    [1,2] = Blue    [2,2] =  ii    [1,3] = Greenish    [2,3] =  3    [1,4] = Yellow    [2,4] =  four    [one,five] = White    [2,5] =  five }        

Accessing Data in Cell Arrays

There are two ways to refer to the elements of a cell array −

  • Enclosing the indices in first bracket (), to refer to sets of cells
  • Enclosing the indices in braces {}, to refer to the information inside individual cells

When you enclose the indices in first subclass, information technology refers to the set of cells.

Cell array indices in smooth parentheses refer to sets of cells.

For case −

c = {'Cherry-red', 'Bluish', 'Green', 'Yellowish', 'White'; one 2 3 4 5}; c(i:2,1:2)        

MATLAB will execute the above statement and render the following result −

ans =  {    [ane,1] = Red    [two,1] =  i    [ane,two] = Blue    [2,two] =  2 }        

You can likewise access the contents of cells by indexing with curly braces.

For example −

c = {'Red', 'Blue', 'Green', 'Xanthous', 'White'; ane 2 iii iv 5}; c{one, 2:four}        

MATLAB will execute the above statement and return the following result −

ans = Blue ans = Green ans = Yellow        

Useful Video Courses


Data Preprocessing for Machine Learning using MATLAB

Video

Complete MATLAB Tutorial: Go from Beginner to Expert

Video

Image Processing Toolbox in MATLAB

Video

Matlab - The Complete Course

Video

Digital Image Processing using MATLAB

Video

Backpropagation Learning Method in Matlab

Video

How to Read in Xls Data to a Cell Array Matlab

Source: https://www.tutorialspoint.com/matlab/matlab_arrays.htm

Post a Comment for "How to Read in Xls Data to a Cell Array Matlab"