how do i create a matrix with an undefined variable?

26 次查看(过去 30 天)
I am trying to create a matrix with the expression "b" in it, without defining a value for "b".
The matrix I want to create is:
0.6b
0
0.8b
Can someone help me with this?
Thank you.

回答(2 个)

John D'Errico
John D'Errico 2016-6-5
编辑:John D'Errico 2016-6-5
Very often people want to create a vector like this, without having a good reason for so doing. In fact, recently I've seen people using symbolic tools when they should not have done so, and another case where they used functions for no good reason, where instead a symbolic solution was absolutely called for. Since we don't know why you are asking to solve this problem, there are valid reasons to question the request and to offer an alternative.
So the functional form as cyclist points out is fine, in SOME cases. It creates a vector that is a function of some unknown variable b.
f = @(b)([0.6*b 0 0.8*b])
This might be useful if you are trying to solve a variety of problems, thus root finding, optimization, etc.
If you have the symbolic TB, then of course nothing stops you from creating the vector
syms b
v = [0.6*b, 0, 0.8*b];
Lacking that TB, then you can use my sympoly toolbox, found on the file exchange.
sympoly b
v = [0.6*b, 0, 0.8*b];
HOWEVER, there are some cases where use of symbolic tools such as these examples is ill-advised. It will produce much slower code. If you don't truly need symbolic tools, then don't use them.
  2 个评论
John Kacuba
John Kacuba 2016-6-5
Thank you - this is helpful. Sorry for my ignorance when it comes to MatLab - I'm new at using it and as a result I'm probably not asking very good questions.
John D'Errico
John D'Errico 2016-6-5
It is not a bad question, though a bit incomplete. The issue is to know when to use a given tool, and I will admit, sometimes that may not be obvious to a new user.
For example, you have a problem with an "unknown" in it. Should the problem be symbolic? Or should it be formulated as a solve problem using a numerical tool, thus anything from fzero, fminbnd, fminsearch, fsolve, fminunc, fmincon, etc?
In some cases multiple approaches work. There are always many ways to solve any problem. But is one solution slower than the other? Will you need to solve a similar problem millions of times? Do you need an analytical solution?
So knowing the goal is an important thing to know how best to formulate the problem.

请先登录,再进行评论。


the cyclist
the cyclist 2016-6-5
编辑:the cyclist 2016-6-5
You could use an anonymous function:
f = @(b)([0.6*b 0 0.8*b])
Then, for example
f(10)
ans =
6 0 8
  2 个评论
John Kacuba
John Kacuba 2016-6-5
Thank you for the response, but I don't think that's exactly what I'm looking for. I am trying to create this 3x1 vector with 'b' still in the vector undefined so I can then multiply it by a matrix. The issue I'm running into is MatLab wants me to define or assign a value to 'b'. I'd like to just keep 'b' in my vector - [0.6b;0;0.8b].

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by