Multiple-Colon
Sean's pick this week is mcolon by Bruno Luong.
% Starting index startidx = [1 4 pi] % Stride between each start and ending index stride = [1 -1 pi] % Ending index endidx = [6 0 pi^2]
startidx = 1.0000 4.0000 3.1416 stride = 1.0000 -1.0000 3.1416 endidx = 6.0000 0 9.8696
% Desired result
disp(v)
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 4.0000 3.0000 2.0000 1.0000 0 3.1416 6.2832 9.4248This can be accomplished with a simple for-loop or some indexing tricks which are sufficiently fast for many applications. However, Bruno's mcolon makes it even faster and more elegant. There are two implementations, one in MATLAB and the other in C++ that can be compiled into a MEX file. To compile the MEX file, you can call the provided mcolon_install. I really like it when files that need any fancy set up come with their own installation function. And now the calling syntax:
v = mcolon(startidx,stride,endidx).'; disp(v)
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 4.0000 3.0000 2.0000 1.0000 0 3.1416 6.2832 9.4248
评论
要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。