简体   繁体   English

Octave / Matlab:扩展向量使其重复自身?

[英]Octave / Matlab: Extend a vector making it repeat itself?

Is there a way to extend a vector by making it repeat itself?有没有办法通过使向量重复自身来扩展向量?

>v = [1 2];
>v10 = v x 5; %x represents some function. Something like "1 2" x 5 in perl

Then v10 would be:那么 v10 将是:

>v10
     1 2 1 2 1 2 1 2 1 2

This should work for the general case, not just for [1 2]这应该适用于一般情况,而不仅仅是 [1 2]

您要查找的函数是repmat()

v10 = repmat(v, 1, 5)

Obviously repmat is the way to go if you know in which direction you want to expand the vector. 如果知道要向哪个方向扩展向量,显然repmat是可行的方法。

However, if you want a general solution that always repeats the vector in the longest direction, this combination of repmat and indexing should do the trick: 但是,如果您希望始终在最长的方向上重复向量的一般解决方案,则repmat和索引的这种组合应该可以解决问题:

 v10=v(repmat(1:length(v),1,5))

Although late, I am posting this because this turned out to be the most popular answer to a similar question here .虽然晚了,但我还是发布了这个,因为事实证明这是对此处类似问题的最受欢迎的答案。

This is a Faster Method Than repmat or reshape by an Order of Magnitude这是一种比repmatreshape快一个数量级的方法

One of the best methods for doing such things is UsingTony's Trick.做这些事情的最好方法之一是使用托尼的把戏。 I came across this trick in one of the Electrical Engineering course lectures notes of Columbia University.我在哥伦比亚大学的电气工程课程讲义之一中遇到了这个技巧。 Repmat and Reshape are usually found to be slower than Tony's trick as it directly uses Matlabs inherent indexing. Repmat 和 Reshape 通常被发现比 Tony 的技巧慢,因为它直接使用 Matlab 的固有索引。 To answer you question,为了回答你的问题,

Lets say, you want to tile the row vector r=[1 2 3] N times like r=[1 2 3 1 2 3 1 2 3...] , then,比方说,您想像r=[1 2 3 1 2 3 1 2 3...]那样平铺行向量r=[1 2 3] N次,然后,

c=r'
cc=c(:,ones(N,1));
r_tiled = cc(:)';

This method has significant time savings against reshape or repmat for large N 's.对于大Nreshaperepmat ,此方法可以显着节省时间。

I conducted a small Matlab test to check the speed differential between repmat and tony's trick .我进行了一个小的 Matlab 测试来检查repmattony's trick之间的速度差异。 Using the code mentioned below, I calculated the times for constructing the same tiled vector from a base vector A=[1:N] .使用下面提到的代码,我计算了从基本向量A=[1:N]构建相同平铺向量的时间。 The results show that YES, Tony's-Trick is FASTER BY AN ORDER of MAGNITUDE, especially for larger N. People are welcome to try it themselves.结果表明,是的,托尼的把戏快了一个数量级,尤其是对于较大的 N。欢迎人们自己尝试。 This much time differential can be critical if such an operation has to be performed in loops.如果必须在循环中执行此类操作,那么这么多的时间差异可能是至关重要的。 Here is the small script I used;这是我使用的小脚本;

N= 10 ;% ASLO Try for values N= 10, 100, 1000, 10000

% time for tony_trick
tic;
A=(1:N)';
B=A(:,ones(N,1));
C=B(:)';
t_tony=toc;
clearvars -except t_tony N

% time for repmat
tic;
A=(1:N);
B=repmat(A,1,N);
t_repmat=toc;
clearvars -except t_tony t_repmat N

The Times (in seconds) for both methods are given below;下面给出了两种方法的时间(以秒为单位);

  • N=10, time_repmat = 8e-5, time_tony = 3e-5 N=10, time_repmat = 8e-5, time_tony = 3e-5
  • N=100, time_repmat = 2.9e-4, time_tony = 6e-5 N=100, time_repmat = 2.9e-4, time_tony = 6e-5
  • N=1000, time_repmat = 0.0302, time_tony = 0.0058 N=1000,time_repmat = 0.0302,time_tony = 0.0058
  • N=10000, time_repmat = 2.9199, time_tony = 0.5292 N=10000,time_repmat = 2.9199,time_tony = 0.5292

My RAM didn't permit me to go beyond N=10000.我的 RAM 不允许我使用超过 N=10000 的 go。 I am sure, the time difference between the two methods will be even more significant for N=100000.我敢肯定,对于 N=100000,这两种方法之间的时间差异会更加显着。 I know, these times might be different for different machines, but the relative difference in order-of-magnitude of times will stand.我知道,这些时间对于不同的机器可能会有所不同,但是时间数量级的相对差异是成立的。 Also, I know, the avg of times could have been a better metric, but I just wanted to show the order of magnitude difference in time consumption between the two approaches.另外,我知道,平均时间可能是一个更好的指标,但我只是想展示两种方法之间时间消耗的数量级差异。 My machine/os details are given below:我的机器/操作系统详细信息如下:

Relevant Machine/OS/Matlab Details : Athlon i686 Arch, Ubuntu 11.04 32 bit, 3gb ram, Matlab 2011b相关机器/操作系统/Matlab 详细信息:Athlon i686 Arch,Ubuntu 11.04 32 位,3gb ram,Matlab 2011b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM