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

3 views (last 30 days)
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.

Accepted Answer

Matt Fig
Matt Fig on 14 Sep 2012
v = [];
v(end+1) = 5;
Why would you want to do this, by the way?
  1 Comment
Leo Simon
Leo Simon on 15 Sep 2012
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!