IF statements, and its outputs.
11 次查看(过去 30 天)
显示 更早的评论
How should I code the if statements so I can organise the 10 outputs of each if statement into a row vector; of which I will further on place into a matrix with a few a few other row vectors?. The Marks row vector is filled prior to the 2 if statements.
Marks = [MarkAnn,MarkBob,MarkJan,MarkJim,MarkJoe,MarkKai,MarkPam,MarkSam,MarkTom,MarkZoe];
Percentages = Marks*10;
for a=1: length(Marks)
if Marks>=5
grade = PASS
else
grade = FAIL
end
end
for a=1: length(Percentages)
if Percentages<50
gpa = FAIL
elseif Percentages>=50, Percentages<65
gpa = PASS
elseif Percentages>=65, Percentages<75
gpa = CREDIT
elseif Percentages>=75, Percentages<85
gpa = DISTINSTION
elseif Percentages>=85
gpa = HIGH_DICTINCTION
end
end
0 个评论
回答(4 个)
Arnaud Miege
2011-8-3
One possible solution:
grade = cell(size(Marks));
for a=1: length(Marks)
if Marks(a)>=5
grade{a} = 'PASS';
else
grade{a} = 'FAIL';
end
end
Use a similar approach for gpa. It might possible to vectorize the for loops.
HTH,
Arnaud
3 个评论
bym
2011-8-10
Percentages=rand(10,1)*100;
gpa = cell(1,length(Percentages));
for a = 1:length(Percentages)
if Percentages(a) < 50
gpa{a} = 'fail';
elseif Percentages(a) >=50 && Percentages(a) < 65
gpa{a} = 'pass';
elseif Percentages(a) >=65 && Percentages(a) < 75
gpa{a} = 'credit';
elseif Percentages(a) >=75 && Percentages(a) < 85
gpa{a} = 'distiction';
else
gpa{a} = 'high distinction';
end %if
end %for
gpa'
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!