Shifting the first element of an array to the end

45 ビュー (過去 30 日間)
Dillon La
Dillon La 2021 年 9 月 6 日
コメント済み: Dillon La 2021 年 9 月 6 日
Hi all, I.m trying to transform vector A into B as below,
A B
[ 1 2 3 4 ] [ 4 1 2 3 ]
Here is what I have got so far, but it didnt work out, please point out where I got it wrong
A = [1 2 3 4];
B = [A(1)];
for i = [2:length(A)]
B = [A(i) B]
end
disp(B);

採用された回答

Matt J
Matt J 2021 年 9 月 6 日
A=[ 1 2 3 4 ];
B=[A(end),A(1:end-1)]
B = 1×4
4 1 2 3
  2 件のコメント
Dillon La
Dillon La 2021 年 9 月 6 日
thanks a bunch!!!
Dillon La
Dillon La 2021 年 9 月 6 日
but how would you do it if the loop starts from the second element?

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

その他の回答 (1 件)

Ive J
Ive J 2021 年 9 月 6 日
a = 1:4;
circshift(a, 1)
ans = 1×4
4 1 2 3
  1 件のコメント
Dillon La
Dillon La 2021 年 9 月 6 日
thank you, but do you have a simpler version of it?
I'm trying to figure out what I got wrong rather than using a new operator

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by