How to extract non-vertical column from matrix in Matlab

泪湿孤枕 提交于 2019-12-02 05:13:47

There may be a more elegant solution, but this works:

b = [1 3 2]';
[rows, cols] = size(A);
A(sub2ind([rows cols], [1 : rows]', b))

As an alternative to @dantswain's solution, you can go to the linear indices directly, assuming you're always selecting from the columns:

r = size(A,1);
A( (1:r).' + (b-1) * r)

This will be faster, but not necessarily clearer.

Unfortunately, there isn't an elegant solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!