2D 矩阵到 3D 矩阵,行到 [row, col] 映射
2D matrix to 3D matrix with row to [row, col] mapping
我有一个二维矩阵,第一维有不同的通道,第二维有时间样本。我想将其重新排列为 3D 矩阵,在第一维和第二维通道中,以及第三个时间样本中。
通道必须根据特定映射进行映射。现在我正在使用 for
-loop 来这样做,但是什么是无循环解决方案?
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, []);
我有一个二维矩阵,第一维有不同的通道,第二维有时间样本。我想将其重新排列为 3D 矩阵,在第一维和第二维通道中,以及第三个时间样本中。
通道必须根据特定映射进行映射。现在我正在使用 for
-loop 来这样做,但是什么是无循环解决方案?
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, []);