if statement matlab problem

I got issue on my code. That I put if statement under the else statement it's return a weird number.
The background here is in1 is for vector of original and resubimission grade. in2 is a character vector with entries reading either 'a' or 'm' , to determine if the two grade would be treat with average or maxium. But one requiurement is that when the resubimission grade is lower than the original grade, it would be picked original grade instead of average grade. Only when resubmission is higher and choose average. I added this argument under the else statement that
if(in1(ct>ct+1))
score=score+ct;
but the result comes out run. How should I fix it.

 Accepted Answer

Rafael S.T. Vieira
Rafael S.T. Vieira on 5 Jun 2020
Edited: Rafael S.T. Vieira on 5 Jun 2020
Please, try to put end's at both if's. The inner if may be capturing the outer if end.

6 Comments

function score=GSquare(in1,in2)
score=0;
ct=1;
for i=1:length(in2)
if(in2(i)=='m')
score=score+max(in1(ct:ct+1));
else
if(in1(ct>ct+1))
score=score+ct;
else
score=score+mean(in1(ct:ct+1));
end
ct=ct+2;
end
score=(score+1.5*in1(end))/length(in2);
end
I put it I think.
% Hi, Maria, I've just added the end command and fixed indentation
function score=GSquare(in1,in2)
score=0;
ct=1;
for i=1:length(in2)
if(in2(i)=='m')
score=score+max(in1(ct:ct+1));
else
if(in1(ct>ct+1))
score=score+ct;
else
score=score+mean(in1(ct:ct+1));
end % <---- this end was missing; please, try it now.
end
ct=ct+2;
end
score=(score+1.5*in1(end))/length(in2);
end
Yes, this is right. But my question is I cannot get an expected result when I run it. The result is same with the result without second if statement. So, just curious is there any coding probelm ?
For example, if in1=[83 44 62 52 87 11 91 12 0 15 56], Underline for first input is original number, second is resubmit number. in2 = ['ammma'] 'a'for average. 'm' for maxium. When do this, because 83>44, which means original grade higher than resbmit grade. It picks 83 instead of average them even though in2='a'. Only when resubmit higher than original it will choose average.
I added this condition under else statement which is ct>ct+1, score=score+ct, means that when original higher than resbmit it's gonna be pick original number. But seems like it's unneccesary. Do you know what's wrong with it?
Another problem that I've noticed is that you are not using indexes properly, when you say in1(c1>c1+1) should actually be in1(c1) > in1(c1+1), and score = score + ct should be score = score +in1(ct). Both if's are necessary due to the requirements that you described. Please, try it now. If it is still not right, please, send a use case with expected input and output (or what is the expected output for the example that you sent). Do you want a vector or a number as output?
function score=GSquare(in1,in2)
score=0;
ct=1;
for i=1:length(in2)
if(in2(i)=='m')
score=score+max(in1(ct:ct+1));
else
if(in1(ct)>in1(ct+1)) % <---- fixed the indexes
score=score+in1(ct); % <---- fixed the index
else
score=score+mean(in1(ct:ct+1));
end % <---- this end was missing; please, try it now.
end
ct=ct+2;
end
score=(score+1.5*in1(end))/length(in2);
end
Hey thanks, it works!!! Appericate.
No problem. Glad to help.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!