필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Milan Kumar
Milan Kumar 2019년 4월 26일
편집: Stephen23 2019년 4월 27일
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
Catalytic
Catalytic 2019년 4월 26일
We have no way of knowing what you consider to be the "right response"....

댓글을 달려면 로그인하십시오.

채택된 답변

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에서 Data Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by