The following code works fine with the first set of input tested however shows error for the second set. I am doing this on Cody. Could anyone tell me what's wrong?
function X = rescale_scores(X)
A= 60:100;
B= rescale(A,0,4);
for c=1:40;
for d=1:size(X,1);
if X(d,length(X))==A(c);
X(d,length(X))=B(c);
end
end
end
X
end

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 7일

2 개 추천

This can be solved using a one-liner
function X = rescale_scores(X)
X(:,end) = interp1([60 100], [0 4], X(:,end));
end

댓글 수: 4

Tavish Pattanayak
Tavish Pattanayak 2020년 5월 8일
Yeah it worked. Thanks!
However could you let me know what was the issue with the code I wrote?
First, In your code, you used length(X) in
if X(d,length(X))==A(c);
X(d,length(X))=B(c);
end
length() is not correct here. Either use size(X,2), or 'end'.
Also, you used
A = 60:100
which will only work if grades are integers. If they have a decimal part, then it will not equal to any element in A and therefore the condition
X(d,length(X))==A(c)
never becomes true.
Tavish Pattanayak
Tavish Pattanayak 2020년 5월 8일
Thank you!
I used the following code. But it doesn't work on Test 1. It shows Assertion failed.
Could anyone help me with this.
function X = rescale_scores(X)
X(:,end)=(X(:,end)-60)/10;
end

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

태그

질문:

2020년 5월 7일

댓글:

2022년 12월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by