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

조회 수: 1 (최근 30일)
Leo Simon
Leo Simon 2012년 9월 14일
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