向量求幂 [英] Vectorized exponentiation

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

问题描述

我有两个向量,碱基的X和指数的N.我想获取X中每个xNn的所有值e = xn的矩阵.

I have two vectors, X of bases and N of exponents. I want to get the matrix of all values e = xn for each x in X and n in N.

例如,以下输入:

X = [2 3 4]'
N = [1 2 3]

应产生:

ans = [2 4 8; 3 9 27; 4 16 64]

有没有一种方法可以不循环进行获取(就像您可以通过使用逐行乘积来获取x×n的所有值一样)?

Is there a way to get this without looping (just like you can get all values of x×n by using the column by row product)?

推荐答案

使用 bsxfun :

bsxfun(@power, X, N)

这假定X是列向量,而N是行向量.如果您想保证这一点,请使用以下更健壮的语法:

This assumes that X is a column vector and N is a row vector. If you want to guarantee that, use the following syntax which is more robust:

bsxfun(@power, X(:), N(:).')

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

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