Circshift the columns of an array with different shiftsize withou using for loop
Afficher commentaires plus anciens
As the tittle suggests I am wondering if it is possible to circshift the columns of an array with a different shiftsize in each column without using a for loop.
Example:
a=randi(10,5,4);
I want to do this
a(:,4)=circshift(a(:,4),[-1 0]);
a(:,3)=circshift(a(:,3),[-2 0]);
without a loop. Is it possible?
6 commentaires
Azzi Abdelmalek
le 15 Juin 2013
a=randi(10,5,4); % the size of a is [5 4] , which means a(:,5) does not exist
Giorgos Papakonstantinou
le 15 Juin 2013
Modifié(e) : Giorgos Papakonstantinou
le 15 Juin 2013
Azzi Abdelmalek
le 15 Juin 2013
But it still not clear, when it starts and when it ends
Giorgos Papakonstantinou
le 15 Juin 2013
Giorgos Papakonstantinou
le 15 Juin 2013
Matt J
le 15 Juin 2013
In your example, you modify 'a' in-place. If that's really what you're after, for-loops are going to be pretty competitive. Notice that no iteration of your loop ever allocates any additional memory larger than 1 column a(:,uu). I think there are tools on the FEX that can get rid of even that.
Réponse acceptée
Plus de réponses (2)
Andrei Bobrov
le 15 Juin 2013
Modifié(e) : Andrei Bobrov
le 16 Juin 2013
[m,n] = size(a);
b = rem(shiftindex-1,m)+1;
c = rem(bsxfun(@plus,m + 1 - b - m*(b == 0),(0:m-1)')-1,m)+1;
out = a(bsxfun(@plus,c,m*(0:n-1)));
4 commentaires
Matt J
le 15 Juin 2013
This solution doesn't use n at all and therefore only seems to make shifted copies of the first column of a:
a =
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
out =
5 4 3 1
1 5 4 2
2 1 5 3
3 2 1 4
4 3 2 5
Matt J
le 15 Juin 2013
I think maybe this was the idea
[m,n] = size(a);
b=mod(bsxfun(@plus,(0:m-1).',-shiftindex(:).' ),m)+1;
b=bsxfun(@plus,b,(0:n-1)*m);
out = a(b)
Giorgos Papakonstantinou
le 16 Juin 2013
Giorgos Papakonstantinou
le 15 Juin 2013
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!