FUNCTION shift of vector's position
17 次查看(过去 30 天)
显示 更早的评论
ANYONE CAN HELP ME PLS TO CREAT THIS FUNCTION I TRIED TO DO IT BUT I COULDNT WITHOUT any toolbox functions of cycles
function rotated = rotateRight( vector, shift )
% Performs cyclic shift of vector's position - the ROR operation.
% Performs horizontal ROR, if a matrix is supplied, rotate all rows.
% The scalar shift specifies how many positions to rotate
% if negative, a ROL operation is performed.
% You cannot use any toolbox functions of cycles,
% just indexing.
0 个评论
采纳的回答
amjad hammad
2021-12-8
编辑:Walter Roberson
2021-12-8
2 个评论
Walter Roberson
2021-12-8
This will work in one direction but not the other:
rotation([shift+1:end, 1:shift]) = vector
更多回答(2 个)
Chunru
2021-12-7
编辑:Chunru
2021-12-7
Here is some pseudo code:
%1. Let the vector index be idx = (0:n-1) + 1
%2. When rotating to right by "shift" (which can be +ve/-ve), the corresponding index becomes
% newIdx = mod((0:n-1) + shift, n) + 1;
% where mod implements the rotation.
%3. Get the rotated vector: y = vector(newIdx)
2 个评论
Walter Roberson
2021-12-7
Take a copy of what is about to be overwriten.
Use array subscripts to move the old data to the new positions. For example
A(:,1:5) = A(:,3:7);
Now write the saved data to the other end of the array, into the spots vacated, like A(:,6:7)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!