QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 1175|回复: 0
打印 上一主题 下一主题

备战美赛 matlab 2013a 操纵多维数组

[复制链接]
字体大小: 正常 放大

3503

主题

538

听众

5990

积分

  • TA的每日心情
    开心
    2017-2-7 15:12
  • 签到天数: 691 天

    [LV.9]以坛为家II

    社区QQ达人 元老勋章 发帖功臣 新人进步奖 优秀斑竹奖 金点子奖 原创写作奖 最具活力勋章 助人为乐奖 风雨历程奖

    群组2013年国赛赛前培训

    群组2014年地区赛数学建模

    群组数学中国第二期SAS培训

    群组物联网工程师考试

    群组2013年美赛优秀论文解

    跳转到指定楼层
    1#
    发表于 2013-7-31 11:27 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    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.
    1. A = [5 7 8;
    2.      0 1 9;
    3.      4 3 6];
    4. A(:,:,2) = [1 0 4;
    5.             3 5 6;
    6.             9 8 7]
    复制代码
    A(:,:,1) =

         5     7     8
         0     1     9
         4     3     6


    A(:,:,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.
    1. B = cat( 3, [2 8; 0 5], [1 3; 7 9], [2 3; 4 6])
    复制代码
    B(:,:,1) =

         2     8
         0     5


    B(:,:,2) =

         1     3
         7     9


    B(:,:,3) =

         2     3
         4     6

    Calls to CAT can be nested.
    1. A = cat(3,[9 2; 6 5], [7 1; 8 4]);
    2. B = cat(3,[3 5; 0 1], [5 6; 2 1]);
    3. 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.
    1. SzA   = size(A)
    2. DimsA = ndims(A)
    3. SzC   = size(C)
    4. DimsC = ndims(C)
    SzA =

         2     2     2


    DimsA =

         3


    SzC =

         2     2     2     3


    DimsC =

         4

    Accessing Elements

    To access a single element of a multidimensional array, use integer subscripts. For example D(1,2,2,22), using D defined in the previous slide, returns 6.

    Array subscripts can also be vectors. For example:
    1. K = C(:,:,1,[1 3])
    复制代码
    K(:,:,1,1) =

         9     2
         6     5


    K(:,:,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.
    1. A = rand(3,3,2);
    2. B = permute(A, [2 1 3]);
    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:
    1. A = cat( 3, [1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], ...
    2.                  [6 4 7; 6 8 5; 5 4 3]);
    3. % The EIG function is applied to each of the horizontal 'slices' of A.
    4. for i = 1:3
    5.     eig(squeeze(A(i,:,:)))
    6. end
    复制代码
    ans =

       10.3589
       -1.0000
        1.6411


    ans =

      21.2293 + 0.0000i
       0.3854 + 1.5778i
       0.3854 - 1.5778i


    ans =

      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.
    1. x1 = -2*pi:pi/10:0;
    2. x2 = 2*pi:pi/10:4*pi;
    3. x3 = 0:pi/10:2*pi;
    4. [x1,x2,x3] = ndgrid(x1,x2,x3);
    5. z = x1 + exp(cos(2*x2.^2)) + sin(x3.^3);
    6. slice(z,[5 10 15], 10, [5 12]); axis tight;
    复制代码
    nddemo_01.png

    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.

    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2025-5-26 00:59 , Processed in 0.587418 second(s), 58 queries .

    回顶部