How can I create and add a variable to a vector?

2 次查看(过去 30 天)
I have an assignment which asks me to take a set of vectors of the form :
3 0 h 0 0
and compute a set of orthonormal vectors such that the span of both sets is the same.
What I need to know is. How can I have the h in the vector as a variable, so that when I apply the algorithm to the vectors it treats the h as a number, but sort of, shows the expression for the result.
Simply entering "a = [1;0;h;0;0]" returns "??? Undefined function or variable 'h'."
I am very new to Matlab so the answer might be very simple, or Matlab just doesn't do it. I don't know.

回答(1 个)

Andrew Newell
Andrew Newell 2011-2-6
If you have the Symbolic Toolbox, you can do this:
syms h
a = [3 0 h 0 0]
If not, you could create a function, for example this inline function:
createVector = @(x) [3 0 x 0 0]
so
a = createVector(1)
give
a =
3 0 1 0 0
In that case you would get a numeric answer for your set of orthonormal vectors.

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by