Better way to use circshift
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to create array B from array A (of course in the real application the numbers are not sequential and the arrays are much biggger i.e. 5000x5000)
B =
5 0 1 2 3 4
4 5 0 1 2 3
3 4 5 0 1 2
2 3 4 5 0 1
1 2 3 4 5 0
A =
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
Here is my solution:
for i = 1:5 B(i,:) = circshift(A(i,:),[0,i]); end
Is there a better (i.e. faster) way to do this? On a more general note, I get the fealing that using a for loop to act on a array is always a bad idea.
2 Kommentare
Antworten (2)
Sean de Wolski
am 1 Nov. 2012
v = 0:5;
B = gallery('circul',v);
B = B(2:end,:);
A = repmat(v,[numel(v)-1,1]);
3 Kommentare
Sean de Wolski
am 1 Nov. 2012
tic,B = gallery('circul',1:5000);toc
Elapsed time is 0.603616 seconds.
Matt J
am 1 Nov. 2012
It seems to be doing better in terms of memory management, compared to past MATLAB versions, but it's still slow. Take the example from my link. In R2012a, I get these timings:
r=[1 2 3 4 sparse(zeros(1,10000))];
tic; A=gallery('circul',r); toc
Elapsed time is 4.887724 seconds.
tic; A=interpMatrix(r,1,length(r),1,'circ').'; toc
Elapsed time is 0.005704 seconds.
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!