circshift function working explanation needed
51 次查看(过去 30 天)
显示 更早的评论
Completely new to matlab. Studying some sample codes.
% bitget and num2str and circular shift
x = 0b10011010u8 % x is 10011010
value3= bitget(x, 8:-1:1) % x's binary representation is 10011010
formatSpec4= '%d'
s4= num2str(value3, formatSpec4);
s5= s4;
s5(1:4) = circshift(s5(1:4),-1);
s6= s5;
Not able to understand the syntax and functionality of circshift. Thank in advance
Please explain me the functionality of circshift.
0 个评论
采纳的回答
Voss
2022-1-16
circshift(s,d) for a vector s and positive integer d shifts the elements of s to the right by d amount, wrapping back to the beginning when they go off the end (thats why it's called circular). If d is negative, elements are shifted to the left by -d amount.
It's easier to see what it does with a few examples in MATLAB rather than a few sentences in English.
s = [10 20 30 40 50] % original vector s
circshift(s,1) % shift s right by 1, last element wraps around to become first
circshift(s,-1) % shift s left by 1, first element wraps around to become last
circshift(s,2) % right by 2
circshift(s,-4) % left by 4
circshift(s,5) % shifting by the length of the vector returns the original vector
In your code, the first 4 elements of s5 are shifted left by one place and stored back into s5 as the first 4 elements (and the remaining elements are untouched):
s5 = [1 2 3 4 5 6 7 8]
s5(1:4) = circshift(s5(1:4),-1)
更多回答(1 个)
Walter Roberson
2022-1-16
M = (10:10:40).' + (1:9)
circshift(M, -1)
You can see that this is the same as
[M(2:end,:); M(1,:)]
And more generally, circshift(M, -K) would be
[M(K+1:end,:); M(1:K,:)]
2 个评论
Walter Roberson
2022-1-16
For vectors:
M = 1 : 9
circshift(M, -1)
which is [M(2:end),M(1)] .
When you specify a scalar for the shift, then circshift operates on the first non-singleton dimension.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Software Development Tools 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!