Cut differently odd and even rows in matrix

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by