create vector from matrix without loop
1 次查看(过去 30 天)
显示 更早的评论
Hi there, I have a quick question concerning MATLAB: given A matrix A(mxn) and a Vector B(m,1) I want to create a vector C(m,1) in which each row element is the row element of A from a comlumn indexed by B. So suppose A = [ 1 2; 3 4; 5 6] and B = [2 1 1]' than C should be C = [2 35]. How can I do this using as few computational power as possible (without loops?) ?
cheers Sebastian
0 个评论
采纳的回答
Azzi Abdelmalek
2013-5-12
A = [ 1 2; 3 4; 5 6]
B = [2 1 1]'
C=A(sub2ind(size(A),(1:size(A,1))',B))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!