필터 지우기
필터 지우기

FUNCTION shift of vector's position

조회 수: 6 (최근 30일)
amjad hammad
amjad hammad 2021년 12월 6일
댓글: Chunru 2021년 12월 9일
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.

채택된 답변

amjad hammad
amjad hammad 2021년 12월 8일
편집: Walter Roberson 2021년 12월 8일
rotated = zeros(1, size(vector, 2))
shift = mod(shift, size(vector, 2))
rotated(1:shift) = vector(end-shift+1:end)
rotated(shift+1:end) = vector(1:end-shift)
i did it in this way but is there anyone have another method how to do it with same conditionals
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 12월 8일
This will work in one direction but not the other:
rotation([shift+1:end, 1:shift]) = vector
amjad hammad
amjad hammad 2021년 12월 8일
thank you it work perfect

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Chunru
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
amjad hammad
amjad hammad 2021년 12월 8일
thanks but i tried what you mean but i get this answer
y =
3 4 5 6 7 8 9 10 1 2
what i need be like this the and will be like arraythis vector 1:10 and shift 2
rotated =
9 10 1 2 3 4 5 6 7 8
Chunru
Chunru 2021년 12월 9일
Then
newIdx = mod((0:n-1) - shift, n) + 1;

댓글을 달려면 로그인하십시오.


Walter Roberson
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)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by