如何在Matlab中从矩阵中提取非垂直列 [英] How to extract non-vertical column from matrix in Matlab
本文介绍了如何在Matlab中从矩阵中提取非垂直列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个矩阵A和一个向量b,它为矩阵的每个对应行指定要提取的元素的列索引.
I have matrix A and a vector b, which specifies column index of the element to be extracted for each corresponding row of the matrix.
例如
A = [1 2 3
4 5 6
7 8 9]
b = [1 3 2]'
我想在输出上显示c = [1 6 8]'
.如何实现呢?
I'd like to have c = [1 6 8]'
on output. How to achieve this?
我尝试了A(:, b)
,但是它并不能按我的需要工作.
I tried A(:, b)
, but it doesn't work as I need.
推荐答案
作为@dantswain解决方案的替代方法,假设您始终从列中进行选择,则可以直接转到线性索引:
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.
这篇关于如何在Matlab中从矩阵中提取非垂直列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文