MATLAB:复制数组的特定部分 [英] MATLAB: copy a specific portion of an array
问题描述
我正在尝试从矩阵中复制一些元素,但不是复制整行,也不是复制单个元素.
I am trying to copy a few elements from a matrix, but not a whole row, and not a single element.
例如,在以下矩阵中:
a = 1 2
3 4
5 6
7 8
9 0
我如何只复制以下数据?
How would I copy out just the following data?
b = 1
3
5
即仅在第1列中的第1:3行...我知道您可以像这样删除整列:
i.e. rows 1:3 in column 1 only... I know that you can remove an entire column like this:
b = a(:,1)
...并且我很感激可以做到这一点,然后转储最后两行,但是由于我正在运行一个非常占用资源的解决方案,因此我想使用更简化的代码.
... and I appreciate that could just do this and then dump the last two rows, but I'd like to use more streamlined code as I am running a very resource-intensive solution.
推荐答案
MATLAB中矩阵中的元素以列优先顺序存储.这意味着,您甚至可以使用单个索引并说:
Elements in a matrix in MATLAB are stored in column-major order. Which means, you could even use a single index and say:
b = a(1:3);
由于前三个元素分别为1,3,5.同样,a(6)是2,a(7)是4,依此类推.请查看sub2ind方法以了解更多信息:
Since the first 3 elements ARE 1,3,5. Similarly, a(6) is 2, a(7) is 4 etc. Look at the sub2ind method to understand more:
http://www.mathworks.com/help/techdoc/ref/sub2ind.html
这篇关于MATLAB:复制数组的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!