Dealing with v(end+1) when v(1) may not have been defined

1 次查看(过去 30 天)
I use the following kind of construction a lot
v(1) = (x+y)^2;
v(end+1) = (x^2 + 2*x*y + y^2)
etc. Sometimes v would be a vector of symbolic expressions, sometimes they are numeric.
I'd like to be able to initialize v in some way so that I could begin with
v(end+1) = (x+y)^2
and thus not treat the first element of the array in a special way.
I.e., I'd like to be able to do something like
v = ''
v(end+1) = (x+y)^2;
v(end+1) = (x^2 + 2*x*y + y^2)
But this doesn't work. Is there a way to do this?
Thanks for any suggestions.

采纳的回答

Matt Fig
Matt Fig 2012-9-14
v = [];
v(end+1) = 5;
Why would you want to do this, by the way?
  1 个评论
Leo Simon
Leo Simon 2012-9-15
Thanks very much, Matt
Reason I do this: Often I define v(1) as some nasty symbolic expression computed by mupad, like simple(diff(some horrible expression)). Once I've got the expression I then simplify it down, keeping a record of my changes in a vector and checking periodically that the elements of the vector give the same answers for random values of the variables involved. Since mupad takes forever to do the computation I want to comment it out once I have the answer. But without your suggestion I'd have to redefine v(1). Now I can do:
v = []
v(end+1) = simple(diff(a mess))
v(end+1) = <output from the diff command>
v(end+1) = <simplification>
and simply comment out the mupad command once I have its answer.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by