Convert 0×1 empty double column vector to zero

I am running a code and some of the parameters are 0×1 empty double column vector. Is there any way to convert these to zero?
I mean isntead of daying empty double, assign zero.

2 comentarios

James Tursa
James Tursa el 30 de Ag. de 2019
We need more details. Are you tring to change the behavior of a function or calculation that returns empty variables? Are you tring to change empty variables in the workspace to 0's? Are these empty variables part of a cell or struct array?
Corinna
Corinna el 15 de Mayo de 2024
just use sum(0×1 empty double column vector)
the result is 0

Iniciar sesión para comentar.

 Respuesta aceptada

James Tursa
James Tursa el 30 de Ag. de 2019
Is this construct all you need?
if( isempty(x) )
x = 0;
end

5 comentarios

Thanks. I have a lot of if in the code. Any other option? preferred to be one command
Write a small function.
function x = replaceWith0IfEmpty(x)
if isempty(x)
x = 0;
end
end
Call that function where you want to replace empty matrices with 0.
y = replaceWith0IfEmpty(y);
James Tursa
James Tursa el 30 de Ag. de 2019
@Zeynab: Again, we need more details. Can you modify the function/calculation that is producing these empty variables?
@james I am doing some calculations for example a=b+c. it is in the loop and c is changing. Sometimes c is "0×1 empty double column vector" and sometimes it is a reall number. When it is empty, I get error in a=b+c. That's why I need to make sure that if c is empty, just replace it with zero. I want to avoid using if conditional.
James Tursa
James Tursa el 31 de Ag. de 2019
What is the calculation you are doing that is producing the empty variable?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 30 de Ag. de 2019

Comentada:

el 15 de Mayo de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by