如果第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
答案 0 :(得分:3)
不确定您要尝试执行的操作是什么,但是我将向量化您的代码,如下所示:
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...