What is this code is trying to do

1 回表示 (過去 30 日間)
Nicle Davidson
Nicle Davidson 2021 年 11 月 25 日
コメント済み: Nicle Davidson 2021 年 11 月 25 日
Can you explain what this code is trying to do?
rot=[2,4,7,3,1,5,6,8]; disp(rot); for i=1:8 rot=mod(rot,8); rot=[rot(8)+1,rot(1:7)+1]; disp(rot); end

採用された回答

Yusuf Suer Erdem
Yusuf Suer Erdem 2021 年 11 月 25 日
rot=[2,4,7,3,1,5,6,8];
disp(rot); %%% Display rot matrix
for i=1:8 %%% this loop will turn 8 times
rot=mod(rot,8); %%% After this operation rot matrix will be---->[2,4,7,3,1,5,6,0] because we are taking its mod according to 8
%%% Each time rot matrix will be renewed (8 times)
rot=[rot(8)+1,rot(1:7)+1]; %%% 'rot(8) + 1' means add rot matrix 8th member 1 (8 times)
%%% 'rot(1:7) + 1' means write rot matrix ' s members 1 to 7 and add 1 to each of them (8 times)
disp(rot); %%% display the final rot matrix (8 times)
end
  2 件のコメント
Rik
Rik 2021 年 11 月 25 日
I would also add that circshift would probably be more efficient, and since the number of shifts is equal to the number of elements, the shifts are only there for display purposes. Without the disp statements the loop would be equivalent to:
rot=mod(rot+8,8);
The +8 can even be removed, as x+8 mod 8 is equivalent to x mod 8.
Nicle Davidson
Nicle Davidson 2021 年 11 月 25 日
Thank you to both answers.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by