repmat vs MATLAB中的简单矩阵乘法 [英] repmat vs simple matrix multiplication in MATLAB

查看:78
本文介绍了repmat vs MATLAB中的简单矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

v为长度为n的行向量.目标是创建一个矩阵A,其中m行均等于v.

Let v be a row vector of length n. The goal is to create a matrix A with m rows that are all equal to v.

MATLAB为此提供了一个名为 repmat 的功能.可能的代码是

MATLAB has a function for this that is called repmat. Possible code would be

A = repmat(v,[m 1])

还有一种使用简单矩阵运算的同样简洁的方法

There is an alternative equally concise way using simple matrix operations

A = ones(m,1)*v

对于大的m和n,这两种方法中的哪一种是优选的吗?

推荐答案

让我们比较一下!

在测试算法时,两个指标很重要:时间和内存.

When testing algorithms 2 metrics are important: time, and memory.

让我们从时间开始:

显然repmat胜了!

Clearly repmat wins!

内存:

profile -memory on
for m=1000:1000:50000
f1=@()(repmat(v,[m 1]));
f2=@()(ones(m,1)*v);
ii=ii+1;
t1(ii)=timeit(f1);
t2(ii)=timeit(f2);
end
profreport

似乎两者占用的内存量相同.但是,探查器以未显示所有内存而闻名 ,所以我们不能完全信任它.

It seems that both take the same amount of memory. However, the profiler is known for not showing all the memory, so we can not fully trust it.

不过,很明显 repmat更好

Still, it is clear that repmat is better

这篇关于repmat vs MATLAB中的简单矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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