Quickest way for alternate indexing a vector

조회 수: 8 (최근 30일)
Nicolas Douillet
Nicolas Douillet 2023년 12월 21일
댓글: Matt J 2023년 12월 21일
Hey,
I am looking for the quickest way to create a vector like this :
u = [5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55]
In which steps 2 and 4 alternate as you can see.
At the moment I manage to do it like this for instance :
u = 1+cumsum(repmat([4 2],[1 9]))
Where n is a given limit value. But this is too slow to me.
Would you know a way to get the same result but quicker ? Perhaps using only Matlab semi colon operator and / or basic math (+,*) operations ? EDIT : especially when u has a lot of elements.
Thank you.
Cheers,
Nicolas
  댓글 수: 2
Mathieu NOE
Mathieu NOE 2023년 12월 21일
hello
IMHO this seems not very slow
n = 1e6;
tic
u = 1+cumsum(repmat([4 2],[1 floor(n/6)]));
toc
Elapsed time is 0.003157 seconds.
size(u)
ans = 1×2
1 333332
Nicolas Douillet
Nicolas Douillet 2023년 12월 21일
편집: Nicolas Douillet 2023년 12월 21일
Thank you all for your interest and solutions :-)
I am going to test them, pick the best in my case, and then choose for the corresponding best answer too.
I actually realize this challenge is very difficult since the goal in my case is nothing less than beating the semi colon operator indexing speed (!)
The only way I guess would be a real/direct semi-colon alternate indexing.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2023년 12월 21일
u = [5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49,53,55]
u = 1×18
5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55
v = 5:2:55;
v(3:3:end) = []
v = 1×18
5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55
  댓글 수: 4
Bruno Luong
Bruno Luong 2023년 12월 21일
version2 needs a finale reshape
Matt J
Matt J 2023년 12월 21일
Yep. I added it.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Matt J
Matt J 2023년 12월 21일
편집: Matt J 2023년 12월 21일
n=9;c=[4;2];
s=c(1)+c(2);
c(2)=s;
u= 1 + c + (0:s:s*(n-1));
u=u(:)'
u = 1×18
5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55

Dyuman Joshi
Dyuman Joshi 2023년 12월 21일
n = 9;
u = [5:6:6*(n-1)+5; 7:6:6*(n-1)+7];
u = reshape(u, 1, [])
u = 1×18
5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55

Matt J
Matt J 2023년 12월 21일
편집: Matt J 2023년 12월 21일
n=9;c=[4;2];
s=c(1)+c(2);
clear u
u(2:2:2*n)=1+s:s:s*n+1;
u(1:2:2*n)=u(2:2:2*n)-c(2)
u = 1×18
5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55

Bruno Luong
Bruno Luong 2023년 12월 21일
편집: Bruno Luong 2023년 12월 21일
Try to do something clever (1st and 2nd methods) but it is slower than your code 53rd method). A small modificationof your code seems to be the most efficient (last method)
test
Elapsed time is 0.028802 seconds. Elapsed time is 0.024224 seconds. Elapsed time is 0.002394 seconds. Elapsed time is 0.001397 seconds.
function test
n=1000000; % array length
tic
u=(5:3:(n-1)*3+5)-mod(0:n-1,2);
toc % Elapsed time is 0.010612 seconds.
tic
u=0:n-1;
u=5+3*u-mod(u,2);
toc % Elapsed time is 0.009249 seconds.
tic % Your method
u = 1+cumsum(repmat([4 2],[1 n/2]));
toc % Elapsed time is 0.002047 seconds.
tic % sligh modification method
u = repmat([4 2],[1 n/2]);
u(1)=5;
u = cumsum(u);
toc % Elapsed time is 0.001861 seconds.
end

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by