找出从给定的行中的最大元素到端 [英] find out the largest element from the given row to the end

查看:111
本文介绍了找出从给定的行中的最大元素到端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个基体和每个列给定的行号码列表

Suppose I have a matrix a and a given row number list for each column:

>> a=magic(5)

>> a =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

>> b=[2 3 4 5 1];  %% Note that the elements in b is arbitrary

现在我想找到列的最大值 1 ,行 2:结束,和列 2 ,行 3:结束,...,和列 5 ,行 1:结束分别,有没有使用循环的方法?我试图 A(B:最后,:),但它从 B(1)启动所有为每列

Now I want to find the maximal values from column 1, row 2:end,and column 2, row 3:end, ... ,and column 5, row 1:end respectively, is there a method without using for-loop? I tried a(b:end,:) but it starts all from b(1) for each column.

推荐答案

设置非想条目 NaN的,然后用最大

Set non-wanted entries to NaN and then use max:

ind = bsxfun(@lt, (1:size(a,1)).', b); %'// logical index
a(ind) = NaN; %// set those entries to NaN
result = max(a); %// compute maximum of each column

这适用于任意 B (不一定是对角型)。

This works for arbitrary b (not necessarily in diagonal form).

这篇关于找出从给定的行中的最大元素到端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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