matlab:循环的棘手矢量化,其中第 n 个值取决于第 n 个和第 n+k 个值
matlab: tricky vectorization of a loop where n-th value in depends on the n-th and n+k-th values
这是一个 matlab 程序的循环版本,适用于 55 个值的数组,棘手的部分是第 n 个值是从第 n 个和 (n+31)-st 更新的,并且在第二种情况下,它是 (n-24)-th.
oldval = rand(1,55);
for j1 = 0:23
new_random = oldval(j1 + 1) - oldval(j1 + 31 + 1);
if (new_random < 0.0)
new_random = new_random + 1.0 ;
end
oldval(j1 + 1) = new_random ;
end
for j1 = 24:54
new_random = oldval(j1 + 1) - oldval((j1 - 24) + 1);
if (new_random < 0.0)
new_random = new_random + 1.0 ;
end
oldval(j1 + 1) = new_random ;
end
我很难对这段代码进行矢量化,我们将不胜感激。
您可以分两个阶段完成,一个用于原始代码的每个循环。但是由于第二个循环中的数据依赖性,您需要将第二个循环进一步分为两个阶段,从而获得与这三个阶段相对应的矢量化代码。这是最终的实现 -
%// Initialize a new array with a copy of the input array
oldval_vect = oldval;
%// Vectorize the first loop that sets the elements 1 to 24
loop1_diff = oldval(1:24) - oldval(32:55);
loop1_add = double(loop1_diff<0) + loop1_diff;
oldval_vect(1:24) = loop1_add;
%// Vectorize the second loop for setting the rest of the elements.
%// Now, within the second loop, there is data dependency after the first
%// 24 elements of the input array are accessed, so we need to break this
%// second loop into two parts, one that sets elements from 25 till 48 and
%// the next one that does from 49 till 55.
loop2_part1_diff = oldval_vect(25:48) - oldval_vect(1:24);
loop2_part1_add = double(loop2_part1_diff<0) + loop2_part1_diff;
oldval_vect(25:48) = loop2_part1_add;
loop2_part2_diff = oldval_vect(49:55) - oldval_vect(25:31);
loop2_part2_add = double(loop2_part2_diff<0) + loop2_part2_diff;
oldval_vect(49:55) = loop2_part2_add;
这是一个 matlab 程序的循环版本,适用于 55 个值的数组,棘手的部分是第 n 个值是从第 n 个和 (n+31)-st 更新的,并且在第二种情况下,它是 (n-24)-th.
oldval = rand(1,55);
for j1 = 0:23
new_random = oldval(j1 + 1) - oldval(j1 + 31 + 1);
if (new_random < 0.0)
new_random = new_random + 1.0 ;
end
oldval(j1 + 1) = new_random ;
end
for j1 = 24:54
new_random = oldval(j1 + 1) - oldval((j1 - 24) + 1);
if (new_random < 0.0)
new_random = new_random + 1.0 ;
end
oldval(j1 + 1) = new_random ;
end
我很难对这段代码进行矢量化,我们将不胜感激。
您可以分两个阶段完成,一个用于原始代码的每个循环。但是由于第二个循环中的数据依赖性,您需要将第二个循环进一步分为两个阶段,从而获得与这三个阶段相对应的矢量化代码。这是最终的实现 -
%// Initialize a new array with a copy of the input array
oldval_vect = oldval;
%// Vectorize the first loop that sets the elements 1 to 24
loop1_diff = oldval(1:24) - oldval(32:55);
loop1_add = double(loop1_diff<0) + loop1_diff;
oldval_vect(1:24) = loop1_add;
%// Vectorize the second loop for setting the rest of the elements.
%// Now, within the second loop, there is data dependency after the first
%// 24 elements of the input array are accessed, so we need to break this
%// second loop into two parts, one that sets elements from 25 till 48 and
%// the next one that does from 49 till 55.
loop2_part1_diff = oldval_vect(25:48) - oldval_vect(1:24);
loop2_part1_add = double(loop2_part1_diff<0) + loop2_part1_diff;
oldval_vect(25:48) = loop2_part1_add;
loop2_part2_diff = oldval_vect(49:55) - oldval_vect(25:31);
loop2_part2_add = double(loop2_part2_diff<0) + loop2_part2_diff;
oldval_vect(49:55) = loop2_part2_add;