简体   繁体   English

如何在MATLAB中从3D矩阵的每一行中选择矢量元素?

[英]How to select vector elements from each row of a 3D matrix in MATLAB?

I have a 3D matrix for example a 1000-by-4-by-20 matrix A and I want to select one of the 1-by-20 vectors from each row of A according to a 1000-by-1 index vector B. This index vector includes numbers form 1 to 4. Also, I do not want to use a for loop. 我有一个3D矩阵,例如1000 x 4 x 20矩阵A,我想根据1000 x 1索引矢量B从A的每一行中选择1 x 20矢量。此索引向量包含1到4的数字。而且,我不想使用for循环。 How should I do that (with and/or without "sub2ind")? 我应该怎么做(有和没有“ sub2ind”)?

Thank you. 谢谢。

I'm not sure this is the most elegant way, but it gets the job done: 我不确定这是最优雅的方法,但是可以完成工作:

[r,c,~]=size(A);
Q=reshape(A,r*c,[]);
P=sub2ind([r,c],[1:r]',B);
result=Q(P,:);

This will put each of the 1x1x20 vectors into the appropriate row in a 1000x20 result matrix. 这会将每个1x1x20向量放入1000x20结果矩阵的相应行中。 You can combine the last two lines if you want. 如果需要,可以合并最后两行。

Here are the first 20 rows of a sample run with the vector B appended to the front to show which column we were supposed to take. 这是样本的前20行,向量B附加在前面,以显示我们应该取哪一列。 The construction of A should be pretty obvious (first digit is the column number, next 3 are the row number - 1. All elements of the 1x1x20 vector are the same.) A的构造应该非常明显(第一个数字是列号,接下来的3个是行号-1。1x1x20向量的所有元素都相同。)

  B       result
  2       2000   2000   2000   2000   2000   2000   2000   2000   2000   2000   2000   2000   2000   2000   2000   2000   2000   2000   2000   2000
  1       1001   1001   1001   1001   1001   1001   1001   1001   1001   1001   1001   1001   1001   1001   1001   1001   1001   1001   1001   1001
  4       4002   4002   4002   4002   4002   4002   4002   4002   4002   4002   4002   4002   4002   4002   4002   4002   4002   4002   4002   4002
  4       4003   4003   4003   4003   4003   4003   4003   4003   4003   4003   4003   4003   4003   4003   4003   4003   4003   4003   4003   4003
  3       3004   3004   3004   3004   3004   3004   3004   3004   3004   3004   3004   3004   3004   3004   3004   3004   3004   3004   3004   3004
  4       4005   4005   4005   4005   4005   4005   4005   4005   4005   4005   4005   4005   4005   4005   4005   4005   4005   4005   4005   4005
  3       3006   3006   3006   3006   3006   3006   3006   3006   3006   3006   3006   3006   3006   3006   3006   3006   3006   3006   3006   3006
  1       1007   1007   1007   1007   1007   1007   1007   1007   1007   1007   1007   1007   1007   1007   1007   1007   1007   1007   1007   1007
  3       3008   3008   3008   3008   3008   3008   3008   3008   3008   3008   3008   3008   3008   3008   3008   3008   3008   3008   3008   3008
  4       4009   4009   4009   4009   4009   4009   4009   4009   4009   4009   4009   4009   4009   4009   4009   4009   4009   4009   4009   4009
  2       2010   2010   2010   2010   2010   2010   2010   2010   2010   2010   2010   2010   2010   2010   2010   2010   2010   2010   2010   2010
  4       4011   4011   4011   4011   4011   4011   4011   4011   4011   4011   4011   4011   4011   4011   4011   4011   4011   4011   4011   4011
  3       3012   3012   3012   3012   3012   3012   3012   3012   3012   3012   3012   3012   3012   3012   3012   3012   3012   3012   3012   3012
  2       2013   2013   2013   2013   2013   2013   2013   2013   2013   2013   2013   2013   2013   2013   2013   2013   2013   2013   2013   2013
  4       4014   4014   4014   4014   4014   4014   4014   4014   4014   4014   4014   4014   4014   4014   4014   4014   4014   4014   4014   4014
  4       4015   4015   4015   4015   4015   4015   4015   4015   4015   4015   4015   4015   4015   4015   4015   4015   4015   4015   4015   4015
  1       1016   1016   1016   1016   1016   1016   1016   1016   1016   1016   1016   1016   1016   1016   1016   1016   1016   1016   1016   1016
  2       2017   2017   2017   2017   2017   2017   2017   2017   2017   2017   2017   2017   2017   2017   2017   2017   2017   2017   2017   2017
  4       4018   4018   4018   4018   4018   4018   4018   4018   4018   4018   4018   4018   4018   4018   4018   4018   4018   4018   4018   4018
  3       3019   3019   3019   3019   3019   3019   3019   3019   3019   3019   3019   3019   3019   3019   3019   3019   3019   3019   3019   3019

Here's a another way using bsxfun : 这是使用bsxfun的另一种方式:

[r, c, t] = size(A);
result = A(bsxfun(@plus, (1:r).'+r*(B-1), (0:t-1)*r*c));

Say the vector is called A . 假设向量称为A You can index it like so: 您可以像这样对它建立索引:

A(1:1000,B,:)

Where index1 can range from 1 to 1000, and index2 can range from 1 to 4. The colon selects everything at that index level. 其中index1的范围是1到1000,而index2的范围是1到4。冒号选择该索引级别的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM