vector indexes for a matrix - MATLAB Cody - MATLAB Central

Problem 44496. vector indexes for a matrix

Difficulty:Rate

Matlab allows us to use several indexing styles making code simpler and easier to read than using loops.

Vectors can have vector subscripts

V = [2 3 4 5 6 7 8 9 10];
idx = [1 3 5];
V(idx)
ans =
   2     4     6

In case of matrices, it allows us to use linear indexing.

M = [2 5 8;
     3 6 9; 
     4 7 10];
idx = [1 3 5];
M(idx) 
ans =
   2     4     6

Given a Matrix M, row and column vectors P,Q, output the elements corresponding to the row and column vectors. Try avoiding the use of loops.

For example (tl;dr)

M = [2 5 8;
     3 6 9; 
     4 7 10];
P = [1 1 2]; Q = [1 3 2];
Matrix_VectorIndexes(M,P,Q)
ans =
   2     4     6

Solution Stats

70.91% Correct | 29.09% Incorrect
Last Solution submitted on Sep 28, 2025

Problem Comments

Solution Comments

Show comments
Join Cody Contest 2025 — Have Fun and Win Prizes!
...
We’re excited to invite you to Cody Contest 2025! 🎉 Pick a team,...
Dive Into Hands-On Learning at MATLAB EXPO 2025 – Register Now!
Get ready to roll up your sleeves at MATLAB EXPO 2025 –...
2

Problem Recent Solvers35

Suggested Problems

Community Treasure Hunt

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

Start Hunting!