Cut differently odd and even rows in matrix

조회 수: 1 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by