Clear Filters
Clear Filters

Accessing alternate rows and columns in the matrix.

30 views (last 30 days)
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
How can I access alternate rows and columns e.g. 1st & third row and 2nd & 4th column from the above matrix?

Accepted Answer

Star Strider
Star Strider on 26 Aug 2022
Edited: Star Strider on 26 Aug 2022
Try this —
A = [ 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16];
Rows = A(1:2:end,:)
Rows = 2×4
1 2 3 4 9 10 11 12
Columns = A(:,2:2:end)
Columns = 4×2
2 4 6 8 10 12 14 16
Both = A(1:2:end, 2:2:end)
Both = 2×2
2 4 10 12
EDIT — (26 Aug 2022 at 12:22)
Corrected typographical error.
.
  2 Comments
Permvir Singh
Permvir Singh on 26 Aug 2022
I am still unable to access whole row and column.
Star Strider
Star Strider on 26 Aug 2022
It works here with the supplied ‘A’ matrix.
I am not certain what matrix you are working with if it is different from ‘A’ here. It could be a problem with the size of the matrix and the ability of whatever you are using to display all the numbers.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!