MATLAB矩陣索引 2

來源:互聯網
上載者:User

使用矩陣的邏輯索引

    在選出A矩陣中與B矩陣內非零值位置相同的元素,為一列向量。所以不是根據值來的,而是根據位置來的。B是一個矩陣由邏輯0和1組成。即B中為非零的位置,就是要選出的A元素位置。

A = [1 2 3; 4 5 6; 7 8 9]
A =
     1     2     3
     4     5     6
     7     8     9

B = logical([0 1 0; 1 0 1; 0 0 1]);
B =
     0     1     0
     1     0     1
     0     0     1
A(B)
ans =
     4
     2
     6
     9


find函數返回B中非零元素的索引,
find(B)
ans =
     2
     4
     8
     9
所以也就是選出A中序號為2 4 8 9的元素,組成列向量。

例1


rand('twister', 5489);    
B = A > 0.5;

A(B) = 0
A =
         0     0.0975    0.1576    0.1419         0
         0     0.2785         0       0.4218    0.0357
    0.1270         0          0            0         0
         0            0    0.4854          0         0
         0            0         0             0         0

例2

A = magic(4)
A =
    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1


B = isprime(A)        %素數
B =
     0     1     1     1
     1     1     0     0
     0     1     0     0
     0     0     0     0

A(~B) = 0;                       % 邏輯索引

A
A =
     0     2     3    13
     5    11     0     0
     0     7     0     0
     0     0     0     0
find(B)
ans =
     2
     5
     6
     7
     9
    13

*使用較小的矩陣作為索引矩陣

大多數情況下,邏輯索引矩陣與對象矩陣大小相同,但索引矩陣也可以小於對象矩陣(不能大於)。

A = [1 2 3;4 5 6;7 8 9]
A =
     1     2     3
     4     5     6
     7     8     9


B = logical([0 1 0; 1 0 1])
B =
     0     1     0
     1     0     1

isequal(numel(A), numel(B))
ans =
     0

A(B)
ans =
     4
     7
     8

MATLAB將索引矩陣中缺少的部分視為0。
以上相當於:

C = logical([B(:);0;0;0]);


isequal(numel(A), numel(C))
ans =
     1

A(C)
ans =
     4
     7
     8

單冒號索引

n = [1 2 3; 4 5 6];
c = {1 2; 3 4};
s = cell2struct(c, {'a', 'b'}, 1);  s(:,2)=s(:,1);
%cell2struct,將c編入結構中,成員變數為a,b,變數值為c,1為個體排列維數。
對每個使用單冒號索引:

n(:)              c{:}             s(:).a
ans =             ans =            ans =
     1                 1                1
     4            ans =            ans =
     2                 3                2
     5            ans =            ans =
     3                 2                1
     6            ans =            ans =
                       4                2

按索引來指定值

   當將一個矩陣的值指定給另一個矩陣時,可以使用任意一種索引方法。
A(J,K,...) = B(M,N,...)  JKMN必須是標量,向量或者矩陣。
A中指定元素個數應該與B中指定元素個數相等。

 

 

Matlab 擷取矩陣的資訊

length:擷取最長一維的長度;

ndimas:返回這是幾維矩陣;

numel:返回元素個數;

size:返回矩陣每一維的長度;

樣本:

先構造一個樣本矩陣:

rand('state', 0); % 初始化隨即數產生器

A = rand(5) * 10;

A(4:5, = []

A =

9.5013 7.6210 6.1543 4.0571 0.5789

2.3114 4.5647 7.9194 9.3547 3.5287

6.0684 0.1850 9.2181 9.1690 8.1317

使用numel例,求矩陣均值

sum(A(:))/numel(A)

ans =

5.8909

使用ndims,size例,找出大於5小於7的矩陣元素

if ndims(A) ~= 2

return

end

[rows cols] = size(A);

for m = 1:rows

for n = 1:cols

x = A(m, n);

if x >= 5 && x <= 7

disp(sprintf('A(%d, %d) = %5.2f', m, n, A(m,n)))

end

end

end

結果為:

A(1, 3) = 6.15

A(3, 1) = 6.07

2 擷取矩陣元素的資料類型

isa:判斷是否是所指定的類型;

iscell/iscellstr/ischar/isfloat/isinteger/isslogical/isnumeric/isreal/isstruct

樣本:

A = [5+7i 8/7 4.23 39j pi 9-2i];

for m = 1:numel(A)

if isnumeric(A(m)) && isreal(A(m))

disp(A(m))

end

end

結果為:

1.1429

4.2300

3.1416

3 判斷是否是特定的結構類型

isempty

isscalar:是否是個標量

issparse:是否是疏鬆陣列

isvector:是否是向量

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.