필터 지우기
필터 지우기

MATLAB does not recognize the matrix

조회 수: 4 (최근 30일)
Mohammad Khan
Mohammad Khan 2020년 5월 9일
편집: Peng Li 2020년 5월 11일
I'm trying to create a GPA calculator for an assignment and I' m making use of matrix ii = 1:n, but MATLAB never makes use of this matrix and instead gives the message that the right and left sides are not compatible.
clc;clear
n = input('Number of courses taken this semester: ');
for kk = 1:n
u(kk) = input('Enter credit hour: ');
letter(kk) = input('Enter letter grade: ', 's');
if letter(kk) == 'A'
score(kk) = '4.00';
elseif letter(kk) == 'A-'
score = '3.67';
elseif letter(kk) == 'B+'
score(kk) = '3.33';
elseif letter(kk) == 'B'
score(kk) = '3.00';
elseif letter(kk) == 'B-'
score(kk) = '2.67';
elseif letter(kk) == 'C+'
score(kk) = '2.33';
elseif letter(kk) == 'C'
score(kk) = '2.00';
elseif letter(kk) == 'D'
score(kk) = '1.00';
elseif letter(kk) == 'F'
score(kk) = '0.00';
end
qlt_pts = u(kk) * score(kk);
end
fprintf('quality points: %f\n',qlt_pts);

답변 (2개)

Peng Li
Peng Li 2020년 5월 9일
qlt_pts = u(kk) * score(kk);
your score is a char array. so what do you want this multiplication to give you?
  댓글 수: 5
Walter Roberson
Walter Roberson 2020년 5월 11일
letter(kk) = string(input('Enter letter grade: ','s'));
Mohammad Khan
Mohammad Khan 2020년 5월 11일
THANK YOU WALTER! THIS IS PRECISELY WHAT I WAS LOOKING FOR!!!!
THAT IS EXACTLY WHAT I WAS LOOKING FOR!!!!

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


Rik
Rik 2020년 5월 9일
As Peng Li alludes to: your scores are char arrays. Why? It makes much more sense to store them are numeric arrays. That way you can also use an assignment like this:
score(kk)=2.33;
I would suggest you add an else with error handling. That way you can catch the user providing an incorrect letter grade. You also need to think if you want to keep all the letters that were entered. If you do, you are probably better off using a cell array.
A last point: if you want to compare char arrays you should be using the strcmp function.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by