How do I define a variable without assigning it a value?

204 ビュー (過去 30 日間)
Ethan Sehrt
Ethan Sehrt 2016 年 6 月 16 日
回答済み: Hoang Truong 2023 年 3 月 19 日
I just want to define a variable, f without assigning any values to it so I can use it in an equation.
  3 件のコメント
João Santos
João Santos 2021 年 12 月 12 日
without syms, whats the best option?
Image Analyst
Image Analyst 2021 年 12 月 12 日
@João Santos, see Star's answer below.

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2016 年 6 月 16 日
You can also assign a variable as an empty matrix, cell, string (or any other empty value):
x = []; % Empty Scalar, Vector Or Matrix
y = {}; % Empty Cell
z = ''; % Empty String Or Character
  5 件のコメント
Rik
Rik 2022 年 9 月 2 日
What is your issue with that? It creates an empty array of the uint8 datatype, exactly as was asked.
Walter Roberson
Walter Roberson 2022 年 9 月 2 日
"exactly" what was asked was "without assigning any values to it so I can use it in an equation"
Assigning an empty array to a variable is assigning a value to it (the empty value), and does not form something that can be used to create equations (in the sense of being able to solve or numerically solve the equation)
The supported answers to create something to be used in "equations" include

サインインしてコメントする。

その他の回答 (5 件)

Lucas Hilden
Lucas Hilden 2020 年 1 月 22 日
have no clue
  2 件のコメント
Allison Noe
Allison Noe 2020 年 11 月 12 日
me too
James Tursa
James Tursa 2020 年 11 月 12 日
MATLAB is a dynamically typed language. You create the variable by assigning it a value. You don't need to create or type it ahead of time like you do in compiled languages such as C/C++.

サインインしてコメントする。


Hoang Truong
Hoang Truong 2023 年 3 月 19 日
syms theta;
R=[cos(theta) sin(theta)];
>> R
R = [cos(theta), sin(theta)]

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 6 月 16 日
You can use a Symbolic Math Toolbox http://www.mathworks.com/help/symbolic/index.html

Walter Roberson
Walter Roberson 2020 年 1 月 22 日
You can use James' File Exchange contribution https://www.mathworks.com/matlabcentral/fileexchange/31362-uninit-create-an-uninitialized-variable-like-zeros-but-faster to create an uninitialized array. The initial contents of the variable will be whatever happened to be in memory,

Walter Roberson
Walter Roberson 2021 年 12 月 12 日
If you are trying to set up an optimization problem, sometimes it is easier to do it by expressing relationships between variables. You might be able to take advantage of Problem Based Optimization
In some cases, equations that you might not think of normally as being optimizations can be rewritten: the expression A(x) == B(x) can potentially be rewritten in terms of minimizing (A(x)-B(x))^2, so if you have the Optimization Toolbox but not the Symbolic Toolbox, then this approach has some potential.

Community Treasure Hunt

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

Start Hunting!

Translated by