How can I add a character string to a numerical array?

I am working on a gradebook problem and I would like to add a column to an array of grades. This column will show the letter 'A' for grades higher than a total of 90. For example, I am using an if/else statement so:
if sum_grades=>90 %add column and set equal to letter A for that specific row of grades. How can I set the new location in the column to letter A? If I set A=1, can I use num2str to get A instead of 1?

 採用された回答

Joseph Cheng
Joseph Cheng 2015 年 5 月 28 日

0 投票

You'll have to work with cells to have a combination of numbers and strings. you cannot have an array of
grade = [ 1 2 3 4 5 6 7 8 'A'];
Is there a reason you cannot create another variable called lettergrade which is a Nx1 array where each row would correspond to the same row of grades you calculated?
also you can get away from the if statement by using find() to find the indexes and use those index locations in another variable or the new cell column

2 件のコメント

Joseph Cheng
Joseph Cheng 2015 年 5 月 28 日
which if you work smart can get a nice table like:
allgrades = randi(15,10,10);
totgrades = sum(allgrades,2)
totgrades(totgrades>100)=100;
lettergrades = num2str(ones(size(totgrades)));
grades = 'ABCD';
scale = 100:-10:60;
for ind = 1:4
lettergrades(totgrades>=scale(ind+1)& totgrades<=scale(ind))=grades(ind);
end
lettergrades(totgrades<60)='F';
gBook = table(allgrades,totgrades,lettergrades)
Perry S
Perry S 2015 年 5 月 28 日
Awesome, thanks for your help Joseph!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Types についてさらに検索

質問済み:

2015 年 5 月 28 日

コメント済み:

2015 年 5 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by