這是一週中我意識到我在MATLAB中理解得有多少的時間。本週,我們有迭代的功課,所以使用for循環和while循環。我目前遇到的問題是我必須寫一個決定誰來僱用某人的函數。我給了一個名單,GPA列表和一個邏輯向量列表,告訴我一個學生是否留下來交談。我必須輸出的是要僱用的人員名單以及他們與招聘人員聊天的時間。使用for while while循環來確定誰要聘請MATLAB
function[candidates_hire, time_spent] = CFRecruiter(names, GPAs, stays_to_talk)
爲了被錄用,一個canidate必須具有高於2.5(不含)的GPA。爲了被僱用,學生必須堅持談話,如果他們不說話,他們不會被錄用。名稱用','分隔,GPA是一個向量。
Time in minutes = (GPA - 2.5) * 4;
到目前爲止我的代碼:
function[candidates_hire, time_spent] = CFRecruiter(names, GPAs, stays_to_talk)
candidates = strsplit(names, ', ');
%// My attempt to split up the candidates names.
%// I get a 1x3 cell array though
for i = 1:length(GPAs)
%// This is where I ran into trouble, I need to separate the GPAs
student_GPA = (GPAs(1:length(GPAs)));
%// The length is unknown, but this isn't working out quite yet.
%// Not too sure how to fix that
return
end
time_spent = (student_GPA - 2.5) * 4; %My second output
while stays_to_talk == 1 %// My first attempt at a while-loop!
if student_GPA > 2.5
%// If the student has a high enough GPA and talks, yay for them
student = 'hired';
else
student = 'nothired'; %If not, sadface
return
end
end
hired = 'hired';
%// Here was my attempt to get it to realize how was hired, but I need
%// to concatenate the names that qualify into a string for the end
nothired = 'nothired';
canidates_hire = [hired];
什麼我的主要問題是,這裏是搞清楚如何讓函數知道他們的名字(1)具有GPA花了通話的時間由以下因素決定(1)。建議我啓動一個計數器,並且我必須確保我的循環保留了它們的名字。有這個問題的任何建議?請和謝謝:)
Test Codes
[Names, Time] = CFRecruiter('Jack, Rose, Tom', [3.9, 2.3, 3.3],...
[false true true])
=> Name = 'Tom'
Time = 3.2000
[Names, Time] = CFRecruiter('Vatech, George Burdell, Barnes Noble',...
[4.0, 2.5, 3.6], [true true true])
=> Name = 'Vatech, Barnes Noble'
Time = 10.4000
一般的建議:計算器不支持正確的代碼高亮MATLAB,用'%//'徵求意見,而不是隻''%你高度提高代碼的可讀性。 – thewaywewalk 2014-10-01 15:00:08
這可以在沒有任何循環的情況下完成。你絕對需要使用'for'和'while'循環嗎? – rayryeng 2014-10-01 15:00:09
夫婦更多問題:「名稱」的輸入類型是什麼?它是名稱的單元數組,其中每個名稱都是一個字符串?你還可以提供一些關於「names」,「GPAs」和「stays_to_talk」的樣子嗎?通常在StackOverflow中,如果您將問題與預期的輸入和輸出結合在一起,人們將能夠更快地回答您的問題。我沒有寫出答案,因爲我無法弄清楚函數的前兩個參數的輸入是什麼。 – rayryeng 2014-10-01 15:01:03