Selecting Elements of a Matrix by Indexing with a Vector
Show older comments
I have a 100x3 matrix and I'd like to select one element from each row to form a 100x1 vector. Given a vector of 100 indeces between 1-3, how can I obtain this? The code below is what I thought should work, but instead produces a 100x100 matrix (concatenation of each 100x1 vector generated by each value of indx).
A = rand(100,3);
indx = randi([1 3],100,1); % for each row, which column do I want to pull the element from?
B = A(:,indx); % B is 100x100, I want 100x1
Accepted Answer
More Answers (1)
David Hill
on 23 Apr 2020
A = rand(100,3);
indx = randi([1 3],100,1);
B = A(1:100,indx);
1 Comment
Darren Wethington
on 23 Apr 2020
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!