Matlab多重索引 [英] Matlab multiple indexing

查看:633
本文介绍了Matlab多重索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的矩阵:

I have a matrix like this one:

a= [ 61 65 63 ; ...
     21 21 24 ; ...
     34 2 51 ]
b= [ -1 0 8; ...
      -2 0 6; ...
      -4 0 2]
c=cat(3,a,b)

我想要做的是通过c上的第一个维度的行找到最高值,然后根据这些值来获取行和列的值,但是在c的第三个维度中。

What I want to do is to find by rows of the first dimension on c the highest value, and then based on these values to get the values at the row and column but in the third dimension of c.

在我的例子中,我正在尝试以下方法:

In my example, I am trying the following:

 [maxV,colIndx] =max(c,[],2)

 m=maxV(:,:,1)
 f=c(:,colIndx(:,1,1),2)

向量m返回我答案的一半,即

The vector m return half of my answer, ie

m =

    65
    24
    51

现在,我希望f类似于

f =

    0
    6
    2

但是,我的代码没有回报我想要的。非常感谢任何帮助!

However, my code doesn't return what I want. Any help is much appreciated!

推荐答案

尝试通过 sub2ind使用线性索引 function:

Try using linear indexing via the sub2ind function:

linIndx = sub2ind(size(c), (1:(size(c,1)))', colIndx(:,:,1), ones(size(c,1),1)*2);
f = c(linIndx)

这篇关于Matlab多重索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆