xlsread and isnan use?

1 次查看(过去 30 天)
Daniel
Daniel 2011-3-17
I'm a student currently working on a MatLab project that requires me to use xlsread to read an xls file that contains a random number of students (rows) and 16 columns (grades of homework/projects/exams/etc) and I am required analyze each students' data (one by one), keep track of each total, and ignore the data that doesn't exist using isnan. The problem is I don't know how I can use isnan and ignore the values that don't exist without changing the values that are 1 to 0. Also, we aren't allowed to use the nansum/nanprod functions either. If this doesn't make much sense, my professor provided an algorithm pseudocode which makes more sense than what I posted up there. Here it is:
% Import Data into MatLab
% type in help xlsread to see how to use this function
% you will need to know how many times you want to repeat the analysis
% run a loop, on each iteration, analyze the data of a SINGLE student
% keep track of the totals for each group of assignment (i.e. PSs, PROJs,...)
% When adding up items, make sure you neglect those that are NaN - find
% them and don't use them - use isnan

回答(1 个)

Laura Proctor
Laura Proctor 2011-3-17
Although using a FOR loop isn't the most effective method, since you can't use nansum, something like this may work:
% set-up some data
n = 5;
grades = randn(n,16);
grades(randi(n,1,3),randi(16,1,4)) = NaN;
finalScore = zeros(n,1);
for idx = 1:n
tempGrades = grades(idx,~isnan(grades(idx,:)));
finalScore(idx,1) = sum(tempGrades);
end
The end result is a column vector containing the point totals for each student.
================
You can use XLSREAD to read in the data from the file like this
data = xlsread('filename.xls');
and then find the dimensions of the data matrix:
[nr nc] = size(data);
and you can then substitute in nr (the number of rows) for n in the above code. As you said before, nc should always be 16, so you don't necessarily need to use this.
  4 个评论
Matt Tearle
Matt Tearle 2011-3-18
Use xlsread to import the numeric data into a matrix, then use the size function to determine the number of rows (n). Then do what Laura said, starting at the line "finalScore = ... " (the lines above that were just to create some example data).
If all you're going to do with the data is sum, then a neat solution is to replace all the NaNs with 0. This can be done in one line with logical indexing. Because I can't in good conscience do your project for you, I'll just show something very similar, and let you work out how to apply it, bearing in mind that isnan is probably useful...
x = randi(10,5)
x(x>6) = -3
Matt Tearle
Matt Tearle 2011-3-18
Wow, I type slowly, I guess. Anyway, to clarify: if you set all the NaNs to zero with logical indexing, then you don't need to do any further testing later - just sum away. Otherwise, do what Laura suggested.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by