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

I just want to define a variable, f without assigning any values to it so I can use it in an equation.

3 comentarios

James Tursa
James Tursa el 16 de Jun. de 2016
Editada: James Tursa el 16 de Jun. de 2016
Do you mean symbolically? E.g.,
syms f
Or maybe you mean an anonymous function? E.g.
myfunction = @(f)f+3
without syms, whats the best option?
@João Santos, see Star's answer below.

Iniciar sesión para comentar.

 Respuesta aceptada

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 comentarios

how can defne int variable?
w = zeros(0,0, 'uint8'); %empty variable but uint8 datatype
or
w = uint8([]); %empty variable but uint8 data type
Chris
Chris el 2 de Sept. de 2022
this just assigns '[]' as the variable lol.
Rik
Rik el 2 de Sept. de 2022
What is your issue with that? It creates an empty array of the uint8 datatype, exactly as was asked.
"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

Iniciar sesión para comentar.

Más respuestas (5)

Lucas Hilden
Lucas Hilden el 22 de En. de 2020
have no clue

2 comentarios

me too
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++.

Iniciar sesión para comentar.

Hoang Truong
Hoang Truong el 19 de Mzo. de 2023
syms theta;
R=[cos(theta) sin(theta)];
>> R
R = [cos(theta), sin(theta)]
Walter Roberson
Walter Roberson el 22 de En. de 2020
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 el 12 de Dic. de 2021
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.

Etiquetas

Preguntada:

el 16 de Jun. de 2016

Respondida:

el 19 de Mzo. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by