arrayfun的使用
Problem 801. Construct an index vector from two input vectors in vectorized fashion
题目大意如下:
给出两个向量,x1定义索引范围的起点,另一个定义方位的终点,即输出 [x1(1):x2(1) x1(2):x2(2) … x1(end):x2(end)]
例子:
x1 = [1 5 12];
x2 = [2 8 21];
结果:
y = [1 2 5 6 7 8 12 13 14 15 16 17 18 19 20 21]
如何不用循环来完成这个问题?我一开始的思路是用递归,写起来也很简单
function y = interleaved_idx(x1,x2)
n = length(x1);
if(n==0) y=[];
else
y = [x1(1):1:x2(1),interleaved_idx(x1(2:end), x2(2:end))];
end
end
53的size,看起来还不错,不过matlab最重要的矢量化思想还是没有体现&