问题描述
我有一个矩阵,例如
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
是第一行的第一元素,5
是第二行的第二元素,7
是第 3 行中的第 1 个元素.
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 索引问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!