フィルターのクリア

row circular shift in matrix

14 ビュー (過去 30 日間)
Abhishek Bakhla
Abhishek Bakhla 2020 年 4 月 20 日
コメント済み: Abhishek Bakhla 2020 年 4 月 24 日
How can I shift all the elements of a particular row in matrix in left circular shift or right circular shift.

採用された回答

Tommy
Tommy 2020 年 4 月 20 日
編集済み: Tommy 2020 年 4 月 23 日
M(row,:) = [M(row,end) M(row,1:end-1)]; % shift to the right
M(row,:) = [M(row,2:end) M(row,1)]; % shift to the left
(edit) To shift by any amount:
M = randi(10,5)
shift = 8; row = 2;
[n,m] = size(M);
M(row,:) = [M(row,(end-mod(shift,m)+1):end) M(row,1:(end-mod(shift,m)))] % shift to the right
M(row,:) = [M(row,(mod(shift,m)+1):end) M(row,1:mod(shift,m))] % shift to the left
(edit) Fixed mistake
  3 件のコメント
Abhishek Bakhla
Abhishek Bakhla 2020 年 4 月 23 日
Hi, it seems to me that shift to left by any amount is not working could you please verify ?
Tommy
Tommy 2020 年 4 月 23 日
Yes sorry I goofed! I used n where I should've used m. Let me know if that fixes it for you.

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

その他の回答 (1 件)

Stephen23
Stephen23 2020 年 4 月 23 日
Simpler using circshift, where k<0 shifts to the left and k>0 shifts to the right:
M(row,:) = circshift(M(row,:),k,2)
  1 件のコメント
Abhishek Bakhla
Abhishek Bakhla 2020 年 4 月 24 日
thank you.

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by