Matlab:矩阵中每一行的Argmax和点积 [英] Matlab: Argmax and dot product for each row in a matrix

查看:165
本文介绍了Matlab:矩阵中每一行的Argmax和点积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个矩阵= X in R^(n*m)W in R^(k*m),其中k<<n. 假设x_i是X的第i行,w_j是W的第j行. 我需要为每个x_i找到最大化<w_j,x_i>

I have 2 matrices = X in R^(n*m) and W in R^(k*m) where k<<n. Let x_i be the i-th row of X and w_j be the j-th row of W. I need to find, for each x_i what is the j that maximizes <w_j,x_i>

我看不到一种方法来遍历X中的所有行,但是有一种方法可以找到最大的点积而不必每次遍历整个W?

I can't see a way around iterating over all the rows in X, but it there a way to find the maximum dot product without iterating every time over all of W?

一个简单的实现是:

n = 100;
m = 50;
k = 10;
X = rand(n,m);
W = rand(k,m);
Y = zeros(n, 1);

for i = 1 : n
  max_ind = 1;
  max_val = dot(W(1,:), X(i,:));
  for j = 2 : k
       cur_val = dot(W(j,:),X(i,:));

       if cur_val > max_val
          max_val = cur_val;
          max_ind = j;
       end

   end

   Y(i,:) = max_ind;
end

推荐答案

点积本质上是矩阵乘法:

Dot product is essentially matrix multiplication:

[~, Y] = max(W*X');

这篇关于Matlab:矩阵中每一行的Argmax和点积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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