本文介绍了Matlab:数值数组索引成字符串数组(无循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在麻省理工学院开放式课程的MATLAB入门课程中遇到了一系列问题。你可以看到它,问题编号9,部分g.iii。

I'm doing a set of problems from the MATLAB's introductory course at MIT OCW. You can see it here, it's problem number 9, part g.iii.

我有一个矩阵,最终成绩为a当然,所有这些都是从1到5。我有另一个数组只有'F''A'(在'递减'订单中)。

I have one matrix with the final grades of a course, all of them range from 1 to 5. And I have another array with only letters from 'F' to 'A' (in a 'decreasing' order).

我知道如何更改矩阵中的元素,我想我可以为每个数字做这样的事情:

I know how to change elements in a matrix, I suppose I could do something like this for each number:

totalGrades(find(totalGrades==1)) = 'F';
totalGrades(find(totalGrades==2)) = 'E';
totalGrades(find(totalGrades==3)) = 'C';
totalGrades(find(totalGrades==4)) = 'B';
totalGrades(find(totalGrades==5)) = 'A';

但是,创建字符串数组字母的目的是什么?

But then, what's the purpose of creating the string array "letters"?

我想过使用一个循环,但我们应该在课程的那个时候解决问题。

I thought about using a loop, but we're supposed to solve the problem without one at that point of the course.

有办法吗?我很高兴知道。这是我整个问题的代码,但我最后一个问题陷入困境。

Is there a way? I'll be glad to know. Here's my code for the whole problem, but I got stuck in that last question.

load('classGrades.mat');
disp(namesAndGrades(1:5,1:8));
grades = namesAndGrades(1:15,2:size(namesAndGrades,2));
mean(grades);
meanGrades = nanmean(grades);
meanMatrix = ones(15,1)*meanGrades;
curvedGrades = 3.5*(grades./meanMatrix);

% Verifying
nanmean(curvedGrades)
mean(curvedGrades)

curvedGrades(curvedGrades>=5) = 5;

totalGrades = nanmean(curvedGrades,2);

letters = 'FDCBA';

非常感谢!

推荐答案

尝试:

letters=['F','D','C','B','A'];
tg = [1 2 1 3 3 1];
letters(tg)

结果:

ans = FDFCCF

这甚至在tg时也能正常工作(总成绩)是一个矩阵:

This works even when tg (total grade) is a matrix:

letters=['F','D','C','B','A'];
tg = [1 2 1 ; 3 3 1];
result = letters(tg);
result


result =

FDF
CCF

编辑(简要说明):


当您执行字母时(2)很容易理解)你得到第二个字母元素( D )。


但你也可以从<$ c中选择几个元素$ c>字母
给它一个数组: letters([1 2])将返回第一个和第二个元素( FD )。


因此, letters(indicesArray)将产生一个长度相同的新数组 indexesArray 。但是,这个数组必须包含从1到字母长度的数字(或者会弹出一个错误)。

Edit (brief explanation):
It is easy to understand that when you do letters(2) you get the second element of letters (D).

But you can also select several elements from letters by giving it an array: letters([1 2]) will return the first and second elements (FD).

So, letters(indexesArray) will result in a new array that has the same length of indexesArray. But, this array has to contain numbers from 1 to the length of letters (or an error will pop up).

这篇关于Matlab:数值数组索引成字符串数组(无循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-09 01:50
查看更多