# IF statements, and its outputs.

조회 수: 10 (최근 30일)
Benaiah 2011년 8월 3일
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
else
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

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

### 답변 (4개)

Arnaud Miege 2011년 8월 3일
One possible solution:
for a=1: length(Marks)
if Marks(a)>=5
else
end
end
Use a similar approach for gpa. It might possible to vectorize the for loops.
HTH,
Arnaud
##### 댓글 수: 3이전 댓글 1개 표시이전 댓글 1개 숨기기
Benaiah 2011년 8월 3일
What u suggested worked, as i now get all the row vectors needed for the matrix in the workspace.
But it returns the error "??? Error using ==> vertcat
CAT arguments dimensions are not consistent." Therefore stopping the output of the matrix.
How can i stop that error from occurring??
Arnaud Miege 2011년 8월 5일
Which line of code does the error point to? Can you post your updated code?

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

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이전 댓글 -2개 표시이전 댓글 -2개 숨기기

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

Walter Roberson 2011년 8월 11일
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Benaiah 2011년 8월 12일
Thanks for all the help guys, i really appreciate it.

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

Benaiah 2011년 8월 11일
Everything works, but it won't give me the output matrix due to the same error of
"??? Error using ==> vertcat CAT arguments dimensions are not consistent."
I know the Marks row vector is causing the problem as the code below demonstrates.
Columns 1 through 9
'Ann' 'Bob' 'Jan' 'Jim' 'Joe' 'Kai' 'Pam' 'Sam' 'Tom'
'FAIL' 'FAIL' 'FAIL' 'FAIL' 'PASS' 'PASS' 'PASS' 'PASS' 'PASS'
'FAIL' 'FAIL' 'FAIL' 'FAIL' 'PASS' 'PASS' 'CREDIT' 'DISTINSTION' 'HIGH_DICTINCTION'
Column 10
'Zoe'
'PASS'
'HIGH_DICTINCTION'
How can i put the Marks row vector into the matrix without the vertcat error occuring.

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

### 카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by