zoukankan      html  css  js  c++  java
  • matlab 字符串、元胞和构架数组

            四种数据类型基本构成比较表
    数组类型     基本组分   组分内涵             基本组分占用字节数
    数值数组      元素     双精度实数标量               8

                      或双精度复数标量              16

    字符串数组     元素     字符                    2 

    元胞数组      元胞    可以存放任何类型、任何大小的数据。     不定

    构架数组      构架    只有挂接在构架上的“域”才能存放数据。    不定

                      数据可以是任何类型、任何大小。

    1.字符串数组:

    a.字符串入门:

    clear
    a=12345.6789
    class(a)
    a_s=size(a)
    a =
    1.2346e+004
    ans =
    double
    a_s =
    1 1

    b.串数组的属性和标志

    a='This is an example.'
    a =
    This is an example.

    a14=a(1:4)
    ra=a(end:-1:1)
    a14 =
    This
    ra =
    .elpmaxe na si sihT

    ascii_a=double(a)
    ascii_a =
    Columns 1 through 12
    84 104 105 115 32 105 115 32 97 110 32 101
    Columns 13 through 19
    120 97 109 112 108 101 46

    char(ascii_a)
    ans =
    This is an example.

    用专门函数char , str2mat , strvcat 创建多行串数组示例:

    S1=char('This string array','has two rows.')
    S1 =
    This string array
    has two rows.
    S2=str2mat('这','字符','串数组','','由4 行组成')
    S2 =

    字符
    串数组


    由 4 行组成

    size(S2)

    ans =

    5 6

    S3=strvcat('这','字符','串数组','','由4 行组成')
    S3 =

    字符
    串数组
    由4 行组成

    size(S3)

    ans =

    4 6

    最常用的数组/字符串转换函数int2str , num2str , mat2str 示例:

    A=eye(2,4);
    A_str1=int2str(A)
    A_str1 =
    1 0 0 0
    0 1 0 0

    rand('state',0)
    B=rand(2,4);
    B3=num2str(B,3)
    B3 =
    0.95    0.607  0.891  0.456
    0.231  0.486  0.762  0.0185

    B_str=mat2str(B,4)
    B_str =
    [0.9501 0.6068 0.8913 0.4565;0.2311 0.486 0.7621 0.0185]

    fprintf, sprintf, sscanf 的用法示例:

    rand('state',0);a=rand(2,2);
    s1=num2str(a)
    s_s=sprintf('%.10e ',a)
    s1 =
    0.95013 0.60684
    0.23114 0.48598
    s_s =
    9.5012928515e-001
    2.3113851357e-001
    6.0684258354e-001
    4.8598246871e-001
    fprintf('%.5g\',a)
    0.95013.23114.60684.48598
    s_sscan=sscanf(s_s,'%f',[3,2])
    s_sscan =

    0.9501 0.4860
    0.2311 0
    0.6068 0

    (2× 2)元胞数组的创建:

    C_str=char('这是','元胞数组创建算例 1');
    R=reshape(1:9,3,3);
    Cn=[1+2i];
    S_sym=sym('sin(-3*t)*exp(-t)');

    (1)直接创建法之一
    A(1,1)={C_str};A(1,2)={R};A(2,1)={Cn};A(2,2)={S_sym}
    A
    A =
    [2x10 char] [3x3 double]
    [1.0000+ 2.0000i] [1x1 sym ]

    (2)直接创建法之二
    B{1,1}=C_str;B{1,2}=R;B{2,1}=Cn;B{2,2}=S_sym;
    celldisp(B)
    B{1,1} =
    这是
    元胞数组创建算例 1
    B{2,1} =
    1.0000 + 2.0000i
    B{1,2} =
    1 4 7
    2 5 8
    3 6 9
    B{2,2} =
    sin(-3*t)*exp(-t)

  • 相关阅读:
    vue的单向数据流
    vue的组件基础
    vue v-for的数组改变导致页面不渲染解决方法
    Binary Heap
    Types of Binary Tree
    Merge Sort
    Master Theorem
    Insertion Sort
    Amazon Redshift and Massively Parellel Processing
    Bubble Sort
  • 原文地址:https://www.cnblogs.com/hxbbing/p/4566559.html
Copyright © 2011-2022 走看看