简体   繁体   English

八度/ Matlab:向向量添加新元素

[英]Octave/Matlab: Adding new elements to a vector

Having a vector x and I have to add an element ( newElem ) . 有一个向量x ,我必须添加一个元素( newElem )。

Is there any difference between - 之间-有什么区别吗?

x(end+1) = newElem; 

and

x = [x newElem];

?

x(end+1) = newElem is a bit more robust. x(end+1) = newElem更加健壮。

x = [x newElem] will only work if x is a row-vector, if it is a column vector x = [x; newElem] x = [x newElem]仅在x为行向量,列向量为x = [x; newElem]时才有效x = [x; newElem] x = [x; newElem] should be used. 应该使用[ x = [x; newElem] x(end+1) = newElem , however, works for both row- and column-vectors. x(end+1) = newElem ,但是对行向量和列向量都适用。

In general though, growing vectors should be avoided. 通常,应避免增长媒介。 If you do this a lot, it might bring your code down to a crawl. 如果您经常执行此操作,则可能会使您的代码难以抓取。 Think about it: growing an array involves allocating new space, copying everything over, adding the new element, and cleaning up the old mess...Quite a waste of time if you knew the correct size beforehand :) 想一想:增加一个数组需要分配新的空间,复制所有内容,添加新的元素以及清理旧的混乱...如果事先知道正确的大小,那会浪费时间:)

Just to add to @ThijsW's answer, there is a significant speed advantage to the first method over the concatenation method: 只是添加到@ThijsW的答案中,第一种方法比串联方法具有明显的速度优势:

big = 1e5;
tic;
x = rand(big,1);
toc

x = zeros(big,1);
tic;
for ii = 1:big
    x(ii) = rand;
end
toc

x = []; 
tic; 
for ii = 1:big
    x(end+1) = rand; 
end; 
toc 

x = []; 
tic; 
for ii = 1:big
    x = [x rand]; 
end; 
toc

   Elapsed time is 0.004611 seconds.
   Elapsed time is 0.016448 seconds.
   Elapsed time is 0.034107 seconds.
   Elapsed time is 12.341434 seconds.

I got these times running in 2012b however when I ran the same code on the same computer in matlab 2010a I get 我在2012b中运行了这些时间,但是当我在Matlab 2010a中的同一台计算机上运行相同的代码时,

Elapsed time is 0.003044 seconds.
Elapsed time is 0.009947 seconds.
Elapsed time is 12.013875 seconds.
Elapsed time is 12.165593 seconds.

So I guess the speed advantage only applies to more recent versions of Matlab 所以我想速度优势只适用于最新版本的Matlab

As mentioned before, the use of x(end+1) = newElem has the advantage that it allows you to concatenate your vector with a scalar, regardless of whether your vector is transposed or not. 如前所述,使用x(end+1) = newElem的优势在于,无论向量是否转置,它都可以将向量与标量连接起来。 Therefore it is more robust for adding scalars. 因此,它在添加标量时更加健壮。

However, what should not be forgotten is that x = [x newElem] will also work when you try to add multiple elements at once. 但是,不应忘记的是,当您尝试一次添加多个元素时, x = [x newElem]也将起作用。 Furthermore, this generalizes a bit more naturally to the case where you want to concatenate matrices. 此外,这对于要连接矩阵的情况更自然一些。 M = [M M1 M2 M3]


All in all, if you want a solution that allows you to concatenate your existing vector x with newElem that may or may not be a scalar, this should do the trick: 总而言之,如果您想要一个允许您将现有向量xnewElem是也可能不是标量的newElem串联的解决方案,这应该可以解决问题:

 x(end+(1:numel(newElem)))=newElem

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

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