Borrar filtros
Borrar filtros

creating a new column in a matrix using 'if condition"

2 visualizaciones (últimos 30 días)
Milan Kumar
Milan Kumar el 26 de Abr. de 2019
Editada: Stephen23 el 27 de Abr. de 2019
On the following matrix:
Untitled.png
I am using the following code which is not giving the right response.
if Reg1(:,3)==0
Reg1(:,10)=0;
else
Reg1(:,10)=a1-b1*Reg1(:,3)+Reg1(:,6);
end
Cannot figure out the issue.
The output is
Untitled.png
  1 comentario
Catalytic
Catalytic el 26 de Abr. de 2019
We have no way of knowing what you consider to be the "right response"....

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 27 de Abr. de 2019
Editada: Stephen23 el 27 de Abr. de 2019
IF will not help you in this situation.
You need to use indexing, e.g.:
Reg1(:,10) = a1-b1*Reg1(:,3) + Reg1(:,6);
idx = Reg1(:,3)==0;
Reg1(idx,10) = 0

Más respuestas (1)

Matt J
Matt J el 26 de Abr. de 2019
Editada: Matt J el 26 de Abr. de 2019
if all( Reg1(:,3)==0 )
Reg1(:,10)=0;
else
Reg1(:,10)=a1-b1*Reg1(:,3)+Reg1(:,6);
end

Categorías

Más información sobre Data Types en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by