【问题标题】:Changing Indices values w.r.t the max of array after an equality check在相等检查后更改索引值 w.r.t 数组的最大值
【发布时间】:2019-03-01 05:39:13
【问题描述】:

我正在使用 Matab 2014a,我有 3 个包含相关值的数组 rx、ry 和 rz,我像这样提取每个数组的最大值的索引:

[~, idx] = max(rx);
[~, idy] = max(ry);
[~, idz] = max(rz);

这3个索引应该是不同的,所以我检查这3个inice之间的相等性,我想要的是idx==idy || idx==idz || idy==idz,我怎样才能为idz取rz的第二个最大值,对于ry和idy也是如此。有什么想法吗?

提前致谢

【问题讨论】:

  • 您想知道这些值是否都不同? result = idx~=idy & idy ~= idz & idz ~= idx;result = all(diff([idx idy idz idx]))
  • 感谢您的回复,我知道如何检查值是否不同,我想要的是 if [code](idx==idy || idx==idz || idy==idz ),如何通过提取 rz 数组的第二个最大值来更改 idz 值,对于 idy 也是如此
  • 嗯,这绝不是你的问题目前所说的。请改写以表明
  • 完成了,我改了标题
  • 这还不够。请更改您的问题以包含您想要的内容,并举例说明。展示你尝试过的东西。这不是一个特别难的问题,但如果你懒得问一个具体的问题,你就不可能得到具体的答案。

标签: arrays matlab indexing max


【解决方案1】:

下面的例子展示了如何检查最大值的索引,如果它们相等则选择下一个:

rx = [9, 8, 7, 6, 5];
ry = [4, 9, 3, 2, 1];
rz = [4, 3, 2, 1, 0];

[~, idx] = max(rx);
[~, idy] = max(ry);
[~, idz] = max(rz);

equal = 1; % flag
while equal
    if idx==idy
        disp(['Max index for ry is the same: ' num2str(idy)]);
        disp ('Changing ry...');
        % Remove the max value
        ry(idy) = 0;
        [~, idy] = max(ry);
        disp (['Recalculating idy for new max: ' num2str(idy)])
    end
    
    if idx==idz || idy==idz
        disp(['Max index for rz is the same: ' num2str(idz)]);
        % Remove the max value
        rz(idz) = 0;
        [~, idz] = max(rz);
        disp (['Recalculating idz for new max: ' num2str(idz)])
    end

    % Check the changes made
    if idx==idy || idx==idz || idy==idz
        equal = 1;
    else
        equal = 0;
    end
end

在 while 循环的第一次迭代中,您将获得

rz 的最大索引相同:1

为新的最大值重新计算 idz:2

因为 idy(4) 和 idx(9)(最大值)都具有索引 1。在第二次迭代期间:

rz 的最大索引相同:2

为新的最大值重新计算 idz:3

idy 和 idz 的值相等,所以再次重新计算 idz。在此之后 3 个索引不同:

idx = 1 id = 2 idz = 3

【讨论】:

  • 谢谢,这正是我所做的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
相关资源
最近更新 更多