简体   繁体   English

2D 矩阵到 3D 矩阵,行到 [row, col] 映射

[英]2D matrix to 3D matrix with row to [row, col] mapping

I have a 2D matrix with in the 1st dimension different channels, and in the 2nd dimension time samples.我有一个二维矩阵,第一维是不同的通道,第二维是时间样本。 I want to rearrange this to a 3D matrix, with in the 1st and 2nd dimension channels, and in the 3rd time samples.我想将其重新排列为 3D 矩阵,在第 1 维和第 2 维通道中以及在第 3 次采样中。

The channels have to mapped according to a certain mapping.通道必须根据某个映射进行映射。 Right now I am using a for -loop to do so, but what would be a no-loop solution?现在我正在使用for循环来这样做,但是什么是无循环解决方案?

N_samples = 1000;
N_channels = 64;

channel_mapping = reshape(1:64, [8 8]).';
% Results in mapping: (can also be random)
%      1     2     3     4     5     6     7     8
%      9    10    11    12    13    14    15    16
%     17    18    19    20    21    22    23    24
%     25    26    27    28    29    30    31    32
%     33    34    35    36    37    38    39    40
%     41    42    43    44    45    46    47    48
%     49    50    51    52    53    55    55    56
%     57    58    59    60    61    62    63    64

data = rand(N_channels, N_samples);

data_grid = NaN(8,8, N_samples);

for k = 1:N_samples
    tmp = data(:, k);
    data_grid(:, :, k) = tmp(channel_mapping);
end

您可以一次性完成,如下所示:

data_grid = reshape(data(channel_mapping, :), 8, 8, []);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM