Cut differently odd and even rows in matrix

2 Ansichten (letzte 30 Tage)
Sergey Makovkin
Sergey Makovkin am 21 Nov. 2016
Bearbeitet: Alexandra Harkai am 21 Nov. 2016
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?

Akzeptierte Antwort

Alexandra Harkai
Alexandra Harkai am 21 Nov. 2016
Bearbeitet: Alexandra Harkai am 21 Nov. 2016
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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by