简体   繁体   中英

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.

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?

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, []);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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