matlab max 最大值不唯一的是否,MATLAB:当有多个最大值时,如何从“max”获取第一个元素而不是随机索引?...

好吧,这有点涉及,但你可以获得所有最大值的索引,然后使用randi和accumarray随机选择一个:

%# (1) Find the maxima

%# if you are interested in the global maximum

%# that may occur multiple times along dimension 6

[maxVal,maxIdx] = max(Defender.Q(:));

%# ALTERNATIVELY

%# if you are interested in local maxima along dimension 6

maxVal = max(Defender.Q,[],6);

maxIdx = find(bsxfun(@eq,Defender.Q,maxVal));

%# (2) pick random maximum for each 5D subarray

%# this assumes that there is no dimension #7 etc

%# In case there is, you need to add a column of ones

%# and then d7 etc to second input of accumarray

%# find row, col, etc subscripts of the maxima

[d1,d2,d3,d4,d5,d6] = ind2sub(size(Defender.Q),maxIdx);

%# create a 5-d array, containing one random index

%# from the maxima along dimension 6, or NaN

randIdx = accumarray([d1,d2,d3,d4,d5],d6,[],@(x)x(randi(length(x))),NaN);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值