Adding names to each row automatically

조회 수: 1 (최근 30일)
DARLINGTON ETAJE
DARLINGTON ETAJE 2019년 8월 28일
댓글: DARLINGTON ETAJE 2019년 8월 28일
Imagine this
A=[2;4;3;1;3;4;1;3];
B=[-1;3;-4;-2;8;-9;1;-5];
E=zeros(8,1);
% such that
for i=1:8
if B(i)<0
E(i)='Bad';
elseif B(i)>0
E(i)='Good';
end
end
D=[A B E];
How do I find D...I want to label each row in D bad or good based te value in B...negative are for Bad while positive are for Good

채택된 답변

Adam Danz
Adam Danz 2019년 8월 28일
편집: Adam Danz 2019년 8월 28일
If you want to form a matrix at the end, you'll need to keep the variables as numeric where "good" equals 1 and "bad" equals 0. That offers other benefits during analysis, too.
E = B > 0;
D=[A B E];
If you'd rather use strings,
E = cell(size(B));
E(B>0) = {'Good'};
E(B<0) = {'Bad'};
Then put it in a table
T = table(A,B,E);
  댓글 수: 2
Adam Danz
Adam Danz 2019년 8월 28일
편집: Adam Danz 2019년 8월 28일
Note that if any elements of B equal 0, they will not be categorized as good or bad. To get around that, you could use <=0 or >=0 depending on how values of 0 would be interpretted.
DARLINGTON ETAJE
DARLINGTON ETAJE 2019년 8월 28일
Don't worry...getting 0 is not allowed in this program

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by