向量化获取每个第 n 个元素(但第 n 个元素是可变的)
Vectorize getting every nth element (but nth element is variable)
如果第 n 个元素是可变的,我如何向量化获取第 n 个元素?
我知道:
A = randi( 10, 10, 2 );
B = A(2:2:end, :); % make another matrix (B) that contains every 2nd element
但是我的 n 个变量改变了。
这是一个基于黄金角度的工作 FOR 循环代码:
1) 它将所需的(黄金角度)度数转换为数组的单元位位置。
2)按给定的数量移动数组。
3) 将第一个移位的单元格放入新数组中。
signal_used_L1 = [1:9](:).';
total_samples = numel( signal_used_L1 );
for hh = 1 : length( signal_used_L1 )
% PHI
deg_to_shift = 137.5077 * hh;
% convert degrees wanted into cell bits
shift_sig_in_bits_L1 = total_samples * deg_to_shift / 360;
% shift signal by given amount of cell bits
shift_sig_L1 = circshift( signal_used_L1(:).' , ...
[0, round(shift_sig_in_bits_L1)] );
% create array with shifted cell bits
sig_bit_built(1, hh) = shift_sig_L1(1, 1);
end
PS: 我正在使用 Octave 4.2.2
不确定你到底想做什么,但我会按如下方式矢量化你的代码:
signal_used_L1 = [1:9](:).';
total_samples = numel( signal_used_L1 );
% PHI
deg_to_shift = 137.5077 * [1:length( signal_used_L1 )];
% convert degrees wanted into cell bits
shift_sig_in_bits_L1 = total_samples * deg_to_shift / 360;
% obtain "wrap-around" indices given above cell bits
indices = mod( -round( shift_sig_in_bits_L1 ), total_samples ) + 1;
% create array with shifted cell bits
signal_used_L1( indices )
顺便说一下,我认为你打算用负偏移做 circshift(即将 "n" 位置移动到 右边 )。在这种情况下,上面的矢量化代码将是 mod( round...
而不是 mod( -round...
如果第 n 个元素是可变的,我如何向量化获取第 n 个元素?
我知道:
A = randi( 10, 10, 2 );
B = A(2:2:end, :); % make another matrix (B) that contains every 2nd element
但是我的 n 个变量改变了。 这是一个基于黄金角度的工作 FOR 循环代码:
1) 它将所需的(黄金角度)度数转换为数组的单元位位置。
2)按给定的数量移动数组。
3) 将第一个移位的单元格放入新数组中。
signal_used_L1 = [1:9](:).';
total_samples = numel( signal_used_L1 );
for hh = 1 : length( signal_used_L1 )
% PHI
deg_to_shift = 137.5077 * hh;
% convert degrees wanted into cell bits
shift_sig_in_bits_L1 = total_samples * deg_to_shift / 360;
% shift signal by given amount of cell bits
shift_sig_L1 = circshift( signal_used_L1(:).' , ...
[0, round(shift_sig_in_bits_L1)] );
% create array with shifted cell bits
sig_bit_built(1, hh) = shift_sig_L1(1, 1);
end
PS: 我正在使用 Octave 4.2.2
不确定你到底想做什么,但我会按如下方式矢量化你的代码:
signal_used_L1 = [1:9](:).';
total_samples = numel( signal_used_L1 );
% PHI
deg_to_shift = 137.5077 * [1:length( signal_used_L1 )];
% convert degrees wanted into cell bits
shift_sig_in_bits_L1 = total_samples * deg_to_shift / 360;
% obtain "wrap-around" indices given above cell bits
indices = mod( -round( shift_sig_in_bits_L1 ), total_samples ) + 1;
% create array with shifted cell bits
signal_used_L1( indices )
顺便说一下,我认为你打算用负偏移做 circshift(即将 "n" 位置移动到 右边 )。在这种情况下,上面的矢量化代码将是 mod( round...
而不是 mod( -round...