gpt4 book ai didi

vectorization - 向量化获取每个第 n 个元素(但第 n 个元素是可变的)

转载 作者:行者123 更新时间:2023-12-01 12:53:31 24 4
gpt4 key购买 nike

如果第 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 )

顺便说一句,我认为你打算用负偏移做环移(即将“n”个位置移动到右边)。在这种情况下,上面的矢量化代码将是 mod( round... 而不是 mod( -round...

关于vectorization - 向量化获取每个第 n 个元素(但第 n 个元素是可变的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59360418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com