Cut differently odd and even rows in matrix

2 ビュー (過去 30 日間)
Sergey Makovkin
Sergey Makovkin 2016 年 11 月 21 日
編集済み: Alexandra Harkai 2016 年 11 月 21 日
Hi, all!
I have a matrix like that:
a = [ 1 2 3 4 5 6 7 8;
9 10 11 12 13 14 15 16;
17 18 19 20 21 22 23 24;
25 26 27 28 29 30 31 32];
I need to get this matrix:
[ 3 4 5 6 7 8;
9 10 11 12 13 14;
19 20 21 22 23 24;
25 26 27 28 29 30];
I want to cut first two values in odd lines and cut last two values in even lines. I means that I need something like this:
a = a(3:8, 1:2:end) && a(1:6, 2:2:end)
But it is incorrect. How can I do it correctly?

採用された回答

Alexandra Harkai
Alexandra Harkai 2016 年 11 月 21 日
編集済み: Alexandra Harkai 2016 年 11 月 21 日
n = size(a, 2); % number of columns
a(1:2:n,:) = a(1:2:n,[[3:n],[1:2]]); % move first 2 elements in odd rows to the end
a = a(:, 1:n-2); % get rid of last 2 columns
Of course prettier to use circshift:
n = size(a, 2); % number of columns
a(1:2:n,:) = circshift(a(1:2:n,:),-2,2); % move first 2 elements in odd rows to the end
a = a(:, 1:n-2); % get rid of last 2 columns

その他の回答 (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