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.

 Akzeptierte Antwort

Rafael S.T. Vieira
Rafael S.T. Vieira am 5 Jun. 2020
Bearbeitet: Rafael S.T. Vieira am 5 Jun. 2020

0 Stimmen

Please, try to put end's at both if's. The inner if may be capturing the outer if end.

6 Kommentare

Maria
Maria am 5 Jun. 2020
Bearbeitet: madhan ravi am 5 Jun. 2020
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.
Rafael S.T. Vieira
Rafael S.T. Vieira am 5 Jun. 2020
Bearbeitet: Rafael S.T. Vieira am 5 Jun. 2020
% 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
Maria
Maria am 5 Jun. 2020
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?
Rafael S.T. Vieira
Rafael S.T. Vieira am 5 Jun. 2020
Bearbeitet: Rafael S.T. Vieira am 5 Jun. 2020
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
Maria
Maria am 5 Jun. 2020
Hey thanks, it works!!! Appericate.
Rafael S.T. Vieira
Rafael S.T. Vieira am 6 Jun. 2020
No problem. Glad to help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by