当前位置:网站首页>Matlab learning notes (4) matlab array
Matlab learning notes (4) matlab array
2022-06-22 02:11:00 【Linest-5】
Catalog
MATLAB Multidimensional arrays
MATLAB Array access data on cells
MATLAB Array
stay MATLAB Variables of all data types in are multidimensional arrays , A vector is a one-dimensional array , A matrix is a two-dimensional array .
First introduce some special arrays .
MATLAB Special arrays in
MATLAB We will use some functions to build some special arrays , For all these functions , A parameter creates a square array , Create a rectangular array with two parameters .
Use zeros() Function to create an array with zero elements :
for example :
zeros(5)MATLAB Execute the above instructions , Return the following results :
Generate a 5 * 5 Of all the 0 matrix .
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 0Use ones() function Create an array :
for example :
ones(4,3)MATLAB Execute the above instructions , Return the following results :
Generate a 4 * 3 Of all the 1 matrix .
ans =
1 1 1
1 1 1
1 1 1
1 1 1Use eye() function Create a matrix :
for example :
eye(4)MATLAB Execute the above instructions , Return the following results :
Generate a 4 * 4 Diagonal matrix of , As the name suggests, it is , At the opposite corners are 1, Related concepts need to be based on linear algebra .
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1Use rand() function Create an array (0,1) A random number that is uniformly distributed on a graph :
for example :
rand(3, 5)MATLAB Execute the above instructions , Return the following results :
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.8003MATLAB Rubik's cube matrix
Produce the same sum , When its elements are added line by line , A column by column or diagonal cube is a square .
Use magic() function Create a magic cube array , It needs a singular Parameters of , This parameter Must be a greater than or equal to 3 scalar .
for example :
magic(4)MATLAB Execute the above instructions , Return the following results :
The resulting matrix can be found , Any line 、 Any column 、 The sum of any diagonal number is 34, This is the magic cube matrix .
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1MATLAB Multidimensional arrays
stay MATLAB in , An array with more than two dimensions is called a multidimensional array .
stay MATLAB The multidimensional array in is an extension of the normal two-dimensional matrix .
MATLAB You need to create a two-dimensional array and then extend it , In this way, a multidimensional array can be generated .
for example , Let's first create a two-dimensional array a.
a = [7 9 5; 6 1 9; 4 3 2]MATLAB Execute the above instructions , Return the following results :
a =
7 9 5
6 1 9
4 3 2
Array a It's a 3x3 array , By the value provided , We can add a third dimension , for example :
a(:, :, 2)= [ 1 2 3; 4 5 6; 7 8 9]MATLAB Execute the above instructions , Return the following results :
a(:,:,1) =
7 9 5
6 1 9
4 3 2
a(:,:,2) =
1 2 3
4 5 6
7 8 9
Again , We can also use ones(), zeros() or rand() Function to create a multidimensional array .
for example :
b = rand(4,3,2)MATLAB Execute the above instructions , Return the following results :
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
Can also use cat() function To create a multidimensional array , It combines a list of arrays along a specified dimension :
cat() The syntax of the function is :
B = cat(dim, A1, A2...)Be careful :
B Is a new array ;
A1, A2, ... Is the array to be connected ;
dim Is the dimension of a series array .
Detailed examples
stay MATLAB Create a script file in , Enter the following code :
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 running this command , Show results :
The matrix a And matrices b Spliced with another unnamed matrix , Form a three-dimensional matrix .
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 9 0
MATLAB Array function
MATLAB The following functions are provided for sorting , rotate , Chaos , Reshape or move the contents of an array .
| function | Purpose |
|---|---|
| length | Vector length or maximum array size |
| ndims | Array dimensions |
| numel | The number of array elements |
| size | Array dimensions |
| iscolumn | Determines whether the input is a column vector |
| isempty | Determines whether the array is empty |
| ismatrix | Determines whether the input is a matrix |
| isrow | Determines whether the input is a line vector |
| isscalar | Determines whether the input is scalar |
| isvector | Determines whether the input is a vector |
| blkdiag | Construct block diagonal matrix from input parameters |
| circshift | Cyclic shift |
| ctranspose | Complex conjugate transpose |
| diag | Diagonal matrix and diagonal of matrix |
| flipdim | Flips the array along the specified dimension |
| fliplr | Flip the matrix from left to right |
| flipud | Flip the matrix down |
| ipermute | n- The inverse permutation dimension of a dimensional array |
| permute | Rearrange N Dimension of dimension group |
| repmat | Copy and tile arrays |
| reshape | Reshape the array |
| rot90 | Rotation matrix 90 degree |
| shiftdim | Shift dimension |
| issorted | Determines whether the collection elements are arranged in sort order |
| sort | Sort the array elements in ascending or descending order |
| sortrows | Sort rows in ascending order |
| squeeze | Delete a single dimension |
| transpose | Transposition |
| vectorize | Vectorization expression |
Detailed examples
Some of the above functions will be illustrated by the following examples .
length , Elements of size and quantity :
stay MATLAB Create a script file in , Enter the following code :
x = [7.1, 3.4, 7.2, 28/4, 3.6, 17, 9.4, 8.9];
length(x) % Array x The length of
y = rand(3, 4, 5, 2);
ndims(y) % Array y Dimensions
s = ['Zara', 'Nuha', 'Shamim', 'Riz', 'Shadab'];
numel(s) % Array s Number of elements of Run the command , The following results are displayed :
ans =
8
ans =
4
ans =
23
Circularly shifted array elements :
stay MATLAB Create a script file in , Enter the following code :
a = [1 2 3; 4 5 6; 7 8 9] % The original matrix y
b = circshift(a,1) % The matrix loops down one row
c = circshift(a,[1 -1]) % The matrix moves down one row , Move one more column to the left
Run the command , The following results are displayed :
a =
1 2 3
4 5 6
7 8 9
b =
7 8 9
1 2 3
4 5 6
c =
8 9 7
2 3 1
5 6 4
MATLAB Array sorting
stay MATLAB Create a script file in , Enter the following code :
v = [ 23 45 12 9 5 0 19 17] % Horizontal array v
sort(v) % An array v Sort
m = [2 6 4; 5 3 9; 2 0 1] % Two dimensional array
sort(m, 1) % Sort by row
sort(m, 2) % Sort by column Run the command , The following results are displayed :
v =
23 45 12 9 5 0 19 17
ans =
0 5 9 12 17 19 23 45
m =
2 6 4
5 3 9
2 0 1
ans =
2 0 1
2 3 4
5 6 9
ans =
2 4 6
3 5 9
0 1 2
MATLAB Cell array
Each cell in the array of cell arrays can store index cells of arrays with different dimensions and data types .
The cell function is used to create a cell array .
The syntax of the cell function is as follows :
C = cell(dim)
C = cell(dim1,...,dimN)
D = cell(obj)Be careful
C Is a cell array ;
dim Is a scalar integer or an integer vector , Specify the cell array C The size of the ;
dim1, ... , dimN Is a scalar integer that specifies the size of C;
obj Is one of the following
Java Array or object
.NET array System.String Type or System.Object
Detailed examples
stay MATLAB Create a script file in , Enter the following code :
c = cell(2, 5);
c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5}Run the command , The following results are displayed :
c =
'Red' 'Blue' 'Green' 'Yellow' 'White'
[ 1] [ 2] [ 3] [ 4] [ 5]
MATLAB Array access data on cells
There are two ways to reference elements of a cell array :
The closed index is in the first bracket (), Is a group of cells
Enclosed in curly braces {}, Index the data in a single cell
Included in the index of the first bracket , It refers to the set of cells .
Cell array index stationary bracket cell set .
for example :
c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5};
c(1:2,1:2)MATLAB Execute the above instructions , Return the following results :
ans =
'Red' 'Blue'
[ 1] [ 2]
You can also use curly braces “{ }” The index accesses the contents of the cell .
for example :
c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5};
c{1, 2:4}MATLAB Execute the above instructions , Return the following results :
ans =
Blue
ans =
Green
ans =
Yellow边栏推荐
- 博途PLC和CODESYS平台下FB编程(如何实现100路FB循环遍历执行)
- GAMES-101-个人总结归纳-Rasterization
- 微信小程序影视评论交流平台系统毕业设计毕设(5)任务书
- Mobile app test method
- Chrome浏览器取消输入框记录表单输入历史
- 本周一问 | -leaf 这个属性的含义?
- 1277_ Implementation analysis of vtaskdelay in FreeRTOS
- Efficient packet processing system based on dpdk
- 小孩子学什么编程?
- Games-101 personal summary rasterization
猜你喜欢
![[Chapter 13 image compression and reconstruction based on Hoffman -- image processing application of MATLAB deep learning practice]](/img/ac/6f3ce735f52bc44a5dd65e0f2b870c.png)
[Chapter 13 image compression and reconstruction based on Hoffman -- image processing application of MATLAB deep learning practice]
![[chapter 02 weight adaptive image denoising technology based on Morphology - full system matlab intelligent driving in-depth learning]](/img/65/c9ba18ffd37f84c3ca399507625c90.png)
[chapter 02 weight adaptive image denoising technology based on Morphology - full system matlab intelligent driving in-depth learning]

digital signal processing

微信小程序影视评论交流平台系统毕业设计毕设(3)后台功能

Mysql数据库轻松学06—数据分析师常用:数据查询语言DQL之单表查询

Chrome browser cancel input box to record form input history

优秀的 Verilog/FPGA开源项目介绍(二十七)- 小型CPU

Mba-day23 at most at least questions - exercises

idea----bookmark
![[chapter 01 image defogging technology based on histogram optimization - full system matlab intelligent driving depth learning]](/img/ba/a63bd93812cabef82a187a361d8487.png)
[chapter 01 image defogging technology based on histogram optimization - full system matlab intelligent driving depth learning]
随机推荐
[Chapter 15 wavelet based image compression technology deep learning machine learning image processing application matlab.]
Test APK exception control WiFi scan attacker development
Shadertoy realizes simple compass
微信小程序影视评论交流平台系统毕业设计毕设(7)中期检查报告
What does the maturity and redemption time of financial products mean?
Mysql数据库轻松学06—数据分析师常用:数据查询语言DQL之单表查询
Brief introduction to common pigtails of communication pigtails
excel常用快捷键excel快捷键汇总
New employees enter the company and learn about work information
Test APK exception control sensor attacker development
What is a neural network
SQL Server recursive query
Chapter 08 handwritten digit recognition based on knowledge base matlab deep learning application practice
es-object vs nested vs has_ child and has_ parent
Chapter 09 English printed character recognition based on feature matching matlab deep learning practical case
digital signal processing
[chapter 07 face QR code recognition based on principal component analysis matlab deep learning practical case]
Common shortcut keys in Excel summary of shortcut keys in Excel
Intel written test questions DIY
微信小程序影视评论交流平台系统毕业设计毕设(1)开发概要