在没有循环的情况下找到两个向量的最大值? [英] Finding maximum of two vectors without a loop?

查看:18
本文介绍了在没有循环的情况下找到两个向量的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有两个向量,比如 xy.

If there are two vectors, say x and y.

for (i in 1:length(x))
   z[i] = max(x[i],y[i])

您能帮我在不使用循环的情况下执行此操作吗?

Can you please help me to perform this without using a loop?

推荐答案

假设向量 xy 的长度相同,pmax是你的功能.

Assuming that the vectors x and y are of the same length, pmax is your function.

z = pmax(x, y)

如果长度不同,由于回收,pmax 表达式将返回与循环不同的值.

If the lengths differ, the pmax expression will return different values than your loop, due to recycling.

这篇关于在没有循环的情况下找到两个向量的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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