Circshift the columns of an array with different shiftsize withou using for loop
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
Matt J
2013-6-15
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.
采纳的回答
更多回答(2 个)
Andrei Bobrov
2013-6-15
编辑:Andrei Bobrov
2013-6-16
[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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!