簡體   English   中英

在MATLAB中將一維索引及其對應的第二維索引列表切成3D矩陣?

[英]Slice a 3D matrix with a list of first-dimension indices and its corresponding second-dimension indices in MATLAB?

如何對具有一維索引及其對應的第二維索引列表的3D矩陣進行切片?

例如,給定

>> A = cat(3, [1 2 3; 4 5 6; 7 8 9], [10 20 30; 40 50 60; 70 80 90], [100 200 300; 400 500 600; 700 800 900])

A(:,:,1) =

     1     2     3
     4     5     6
     7     8     9


A(:,:,2) =

    10    20    30
    40    50    60
    70    80    90


A(:,:,3) =

   100   200   300
   400   500   600
   700   800   900

我想切出A(2, 3, :)A(1, 2, :)得到[6 60 600; 2 20 200] [6 60 600; 2 20 200]

我失敗了

>> A([2, 1], [3, 2], :)

ans(:,:,1) =

     6     5
     3     2


ans(:,:,2) =

    60    50
    30    20


ans(:,:,3) =

   600   500
   300   200

我相信有一個單線/優雅的解決方案。

以提取所需的元素下標應轉換為索引( sub2ind ),但在此之前它在3D轉置( permute應適用),以使第三尺寸變為第一。

idx = [2 3; 1 2];
[m n z]= size(A);
B=permute(A,[3 1 2]);
result = B(:,sub2ind([m,n],idx(:,1),idx(:,2)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM