Matlab:创建行向量相同的矩阵.使用repmat()或乘以ones() [英] Matlab: create matrix whose rows are identical vector. Use repmat() or multiply by ones()
问题描述
我想通过将向量连接到自身n次来从向量创建矩阵.因此,如果我的向量是mx1
,那么我的矩阵将是mxn
,并且矩阵的每一列都将等于该向量.
I want to create a matrix from a vector by concatenating the vector onto itself n times. So if my vector is mx1
, then my matrix will be mxn
and each column of the matrix will be equal to the vector.
以下哪个是最好/正确的方法,或者还有我不知道的更好的方法?
Which of the following is the best/correct way, or maybe there is a better way I do not know?
matrix = repmat(vector, 1, n);
matrix = vector * ones(1, n);
谢谢
推荐答案
Here is some benchmarking using timeit
with different vector sizes and repetition factors. The results to be shown are for Matlab R2015b on Windows.
首先为每种考虑的方法定义一个函数:
First define a function for each of the considered approaches:
%// repmat approach
function matrix = f_repmat(vector, n)
matrix = repmat(vector, 1, n);
%// multiply approach
function matrix = f_multiply(vector, n)
matrix = vector * ones(1, n);
%// indexing approach
function matrix = f_indexing(vector,n)
matrix = vector(:,ones(1,n));
然后生成不同大小的向量,并使用不同的重复因子:
Then generate vectors of different size, and use different repetition factors:
M = round(logspace(2,4,15)); %// vector sizes
N = round(logspace(2,3,15)); %// repetition factors
time_repmat = NaN(numel(M), numel(N)); %// preallocate results
time_multiply = NaN(numel(M), numel(N));
time_indexing = NaN(numel(M), numel(N));
for ind_m = 1:numel(M);
for ind_n = 1:numel(N);
vector = (1:M(ind_m)).';
n = N(ind_n);
time_repmat(ind_m, ind_n) = timeit(@() f_repmat(vector, n)); %// measure time
time_multiply(ind_m, ind_n) = timeit(@() f_multiply(vector, n));
time_indexing(ind_m, ind_n) = timeit(@() f_indexing(vector, n));
end
end
结果绘制在以下两个图中,使用repmat
作为参考:
The results are plotted in the following two figures, using repmat
as reference:
figure
imagesc(time_multiply./time_repmat)
set(gca, 'xtick',1:2:numel(N), 'xticklabels',N(1:2:end))
set(gca, 'ytick',1:2:numel(M), 'yticklabels',M(1:2:end))
title('Time of multiply / time of repmat')
axis image
colorbar
figure
imagesc(time_indexing./time_repmat)
set(gca, 'xtick',1:2:numel(N), 'xticklabels',N(1:2:end))
set(gca, 'ytick',1:2:numel(M), 'yticklabels',M(1:2:end))
title('Time of indexing / time of repmat')
axis image
colorbar
也许更好的比较是针对每个测试的向量大小和重复因子,三种方法中最快的一种:
Perhaps a better comparison is to indicate, for each tested vector size and repetition factor, which of the three approaches is the fastest:
figure
times = cat(3, time_repmat, time_multiply, time_indexing);
[~, fastest] = min(times, [], 3);
imagesc(fastest)
set(gca, 'xtick',1:2:numel(N), 'xticklabels',N(1:2:end))
set(gca, 'ytick',1:2:numel(M), 'yticklabels',M(1:2:end))
title('1: repmat is fastest; 2: multiply is; 3: indexing is')
axis image
colorbar
一些结论可以从图中得出:
- 基于乘法的方法总是比
repmat
慢
- 基于索引的方法类似于
repmat
.对于向量大小或重复因子较大的值,它往往更快,而对于较小的值,它的速度往往更慢.
- The multiply-based approach is always slower than
repmat
- The indexing-based approach is similar to
repmat
. It tends to be faster for large values of vector size or repetition factor, and slower for small values.
这篇关于Matlab:创建行向量相同的矩阵.使用repmat()或乘以ones()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!