フィルターのクリア

how to rotate integer vector from right to left and vis versa

4 ビュー (過去 30 日間)
Abdullah Sultan
Abdullah Sultan 2021 年 11 月 7 日
コメント済み: DGM 2021 年 11 月 8 日
Write afunction called rotleft that will receiveone row vector as an argument (you mayassume that it is a row vector with a length of at least two) and willreturn another vector, whichconsists of the input vector rotatedto the left—e.g., all values shift over one element, and the first element iswrapped around to theend. For example,
>> rotleft([1 3 4]) ans =
3 4 1
could anyone help please ?
  3 件のコメント
Abdullah Sultan
Abdullah Sultan 2021 年 11 月 7 日
Dear TADA, thank you for your concern, actually it's not hm, I have finished the Matlab subject in last year . Actually, now I train myself. I make a revision to apply for the MATHWORK official exam. Thank you again
Abdullah Sultan
Abdullah Sultan 2021 年 11 月 8 日
checkout this and give me your opinion
function Rotatvec = vecRot(vec)
% this function is to rotate vector
% the maximum number mudt user enter is 18
vec= input('enter your max number from 1 to 18:')
vecRot= [20:-1:vec]
end

サインインしてコメントする。

採用された回答

DGM
DGM 2021 年 11 月 7 日
Something like this
shamount = 1; % amount to shift left (negative to shift right)
A = 1:5 % test vector
A = 1×5
1 2 3 4 5
B = circshift(A,[0 -shamount]) % using circshift
B = 1×5
2 3 4 5 1
C = A(mod((1:numel(A))+shamount-1,numel(A))+1) % using basic indexing
C = 1×5
2 3 4 5 1
  2 件のコメント
Abdullah Sultan
Abdullah Sultan 2021 年 11 月 8 日
thank you for your help
i'd like to present my work and give me your opinoin
function Rotatvec = vecRot(vec)
% this function is to rotate vector
% the maximum number mudt user enter is 18
vec= input('enter your max number from 1 to 18:')
vecRot= [20:-1:vec]
end
DGM
DGM 2021 年 11 月 8 日
Pass parameters as input arguments instead of pestering the user for interactive inputs.
The result is assigned to a variable with the same name as the function instead of the output argument.
The rotation method doesn't do what you think it does. Consider:
vec = [12 15 20 17]
vec = 1×4
12 15 20 17
B = [20:-1:vec] % just creates a linear vector from 20 to vec(1)
B = 1×9
20 19 18 17 16 15 14 13 12
I already mentioned ways to do this.
myvecrot(1:10,2)
ans = 1×10
3 4 5 6 7 8 9 10 1 2
myvecrot(1:10,-2)
ans = 1×10
9 10 1 2 3 4 5 6 7 8
function outvec = myvecrot(invec,k)
outvec = invec(mod((1:numel(invec))+k-1,numel(invec))+1);
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by