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 계정에 로그인하거나 계정을 새로 만드십시오.