1112 0

[Weka及其他] MATLAB课程:代码示例之Working with Arrays and Matrices(四) [推广有奖]

企业贵宾

已卖:160份资源

巨擘

0%

还不是VIP/贵宾

-

威望
4
论坛币
624047 个
通用积分
180.5582
学术水平
918 点
热心指数
987 点
信用等级
841 点
经验
399203 点
帖子
9786
精华
48
在线时间
17322 小时
注册时间
2014-8-19
最后登录
2022-11-2

楼主
widen我的世界 学生认证  发表于 2016-3-7 12:01:24 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

MATLAB课程:代码示例之Working with Arrays and Matrices(四)

Manipulating Multidimensional Arrays


This example shows how to work with arrays having more than two dimensions. Multidimensional arrays can be numeric, character, cell, or structure arrays.

Multidimensional arrays can be used to represent multivariate data. MATLAB® provides a number of functions that directly support multidimensional arrays.


Creating Multi-Dimensional Arrays

Multidimensional arrays in MATLAB are created the same way as two-dimensional arrays. For example, first define the 3 by 3 matrix, and then add a third dimension.

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


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


The CAT function is a useful tool for building multidimensional arrays. B = cat(DIM,A1,A2,...) builds a multidimensional array by concatenating A1, A2 ... along the dimension DIM.

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


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


Calls to CAT can be nested.

A = cat(3,[9 2; 6 5], [7 1; 8 4]);B = cat(3,[3 5; 0 1], [5 6; 2 1]);C = cat(4,A,B,cat(3,[1 2; 3 4], [4 3; 2 1]));


Finding the Dimensions

SIZE and NDIMS return the size and number of dimensions of matrices.

SzA   = size(A)DimsA = ndims(A)SzC   = size(C)DimsC = ndims(C)


SzA =     2     2     2DimsA =     3SzC =     2     2     2     3DimsC =     4


Accessing Elements

To access a single element of a multidimensional array, use integer subscripts. For example, using A defined from above, A(1,2,2) returns 1.

Array subscripts can also be vectors. For example:

K = C(:,:,1,[1 3])


K(:,:,1,1) =     9     2     6     5K(:,:,1,2) =     1     2     3     4


Manipulating Multi-Dimensional Arrays

RESHAPE, PERMUTE, and SQUEEZE are used to manipulate n-dimensional arrays. RESHAPE behaves as it does for 2D arrays. The operation of PERMUTE is illustrated below.

Let A be a 3 by 3 by 2 array. PERMUTE(A,[2 1 3]) returns an array with the row and column subscripts reversed (dimension 1 is the row, dimension 2 is the column, dimension 3 is the depth and so on). Similarly, PERMUTE(A,[3,2,1]) returns an array with the first and third subscripts interchanged.

A = rand(3,3,2);B = permute(A, [2 1 3]);C = permute(A, [3 2 1]);


Selecting 2D Matrices From Multi-Dimensional Arrays

Functions like EIG that operate on planes or 2D matrices do not accept multi-dimensional arrays as arguments. To apply such functions to different planes of the multidimensional arrays, use indexing or FOR loops. For example:

A = cat( 3, [1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], ...   [6 4 7; 6 8 5; 5 4 3]);% The EIG function is applied to each of the horizontal 'slices' of A.for i = 1:3   eig(squeeze(A(i,:,:)))end


ans =   10.3589   -1.0000    1.6411ans =  21.2293 + 0.0000i   0.3854 + 1.5778i   0.3854 - 1.5778ians =  13.3706 + 0.0000i  -1.6853 + 0.4757i  -1.6853 - 0.4757i


INTERP3, INTERPN, and NDGRID are examples of interpolation and data gridding functions that operate specifically on multidimensional data. Here is an example of NDGRID applied to an N-dimensional matrix.

x1 = -2*pi:pi/10:0;x2 = 2*pi:pi/10:4*pi;x3 = 0:pi/10:2*pi;[x1,x2,x3] = ndgrid(x1,x2,x3);z = x1 + exp(cos(2*x2.^2)) + sin(x3.^3);slice(z,[5 10 15], 10, [5 12]);axis tight


You can build multidimensional cell arrays and multidimensional structure arrays, and can also convert between multidimensional numeric and cell arrays.

To find out more, consult the MATLAB documentation on multidimensional arrays.



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Matrices MATLAB课程 matrice working matric MATLAB课程 代码示例 Working with Arrays


https://www.cda.cn/?seo-luntan
高薪就业·数据科学人才·16年教育品牌

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-12 05:58