问题描述
我有一个矩阵,例如
A = [ 1 2 3; 4 5 6; 7 8 9] ;
和一个大小为1x3的向量,它指定每行中哪个元素是我要查找的元素 - 即如果
and a vector of size 1x3 which specifies which element in each row is the one I'm looking for - i.e. If
vector = [ 1 2 1 ]
然后所需的输出是
[ 1 5 7 ]
因为 1
是1中的第1个元素'st row, 5
是第2行中的2',而 7
是第1行第3行中的元素。
since 1
is the 1'st element in the 1'st row, 5
is the 2'nd in the 2'nd row, and 7
is the 1'st element in the 3'rd row.
我如何实现这一目标?无法找到内置函数来做到这一点,这让我感到惊讶。
How do I achieve this? Couldn't find a built in function to do this, which surprised me.
推荐答案
首先,Matlab中的索引去了从上到下。
所以在你的情况下 A [1] = 1,A [2] = 4,A [3] = 7
First of all, the indexes in Matlab go from top to bottom.
So in your case A[1] = 1 , A[2] = 4 , A[3] = 7
也就是说,在A'上工作会更容易,因为它有点琐碎。
That said, it would be easier to work on A' , because its a bit more trivial.
B = A';
B((vector + [0:2].* 3))
这篇关于MATLAB索引问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!