How to perform circshift on specific elements?

6 ビュー (過去 30 日間)
hbcukid
hbcukid 2020 年 11 月 20 日
コメント済み: hbcukid 2020 年 11 月 20 日
I have a much larger dataset but given A = [1 2 3 4; 5 6 7 8; 9 10 11 12], how can I use circshift on the odd rows and only columns 2 - 3 to move the values one column to the left. I know those values are indexed by A = A(1:2:end, 2:3); and the circshift should be circshift(A, [0 -1]) but I am having trouble putting it all together.

採用された回答

Rik
Rik 2020 年 11 月 20 日
You are overwriting the original array, instead of using circshift on the partial array.
A = [1 2 3 4; 5 6 7 8; 9 10 11 12];
L = false(size(A));
L(1:2:end, 2:3) = true;
A_temp = A(L);
A_temp = circshift(A_temp, [0 -1]);
A(L) = A_temp;
%Or with more compact notation:
A = [1 2 3 4; 5 6 7 8; 9 10 11 12];
A(1:2:end, 2:3) = circshift(A(1:2:end, 2:3), [0 -1]);
disp(A)
1 3 2 4 5 6 7 8 9 11 10 12
  1 件のコメント
hbcukid
hbcukid 2020 年 11 月 20 日
Wow, thank you.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by