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

2 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2019-4-27
编辑:Stephen23 2019-4-27
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

更多回答(1 个)

Matt J
Matt J 2019-4-26
编辑:Matt J 2019-4-26
if all( Reg1(:,3)==0 )
Reg1(:,10)=0;
else
Reg1(:,10)=a1-b1*Reg1(:,3)+Reg1(:,6);
end

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by