Why are my staitistic functions returning an array rather than a single value?

3 次查看(过去 30 天)
I've been trying to get my function to return a single value, but no matter how I arrange it the statisic function(x) always comes back as the same array that it was sent in as. What seems to be the problem with my code?
file = input('Please input a file name. ', 's');
while exist(file)==0
file = input('That file does not exist, please input another file name. ', 's');
end
filename = load(file);
[Rows, Columns] = size(filename);
if Rows == 2
x= filename(1:Rows,:);
elseif Columns == 2
x = filename(:,1:Columns);
else
disp('This file is not stored in data or rows, please input a different data file.')
end
xMin = min(x)
xMax = max(x)
xMean = mean(x)
Returns:
xMin =
Columns 1 through 19
2 8 4 6 1 3 5 7 9 10 11 2 4 6 3 5 6 8 9
Columns 20 through 26
10 11 5 6 7 2 3
xMax =
Columns 1 through 19
71 58 75 90 82 85 89 85 94 90 81 97 94 93 65 48 62 80 85
Columns 20 through 26
90 86 85 78 79 60 89
xMean =
Columns 1 through 11
36.5000 33.0000 39.5000 48.0000 41.5000 44.0000 47.0000 46.0000 51.5000 50.0000 46.0000
Columns 12 through 22
49.5000 49.0000 49.5000 34.0000 26.5000 34.0000 44.0000 47.0000 50.0000 48.5000 45.0000
Columns 23 through 26
42.0000 43.0000 31.0000 46.0000

采纳的回答

Star Strider
Star Strider 2020-9-27
The min, max, and mean functions operate on the first dimension that is greater than 1, so for a matrix, they will all produce a row vector for output.
The easiest way to get those for the entire matrix is to use the (:) subscript operator to create a column vector, and use that as the argument to the functions:
x = randn(5);
meanx = mean(x(:));
and so for the others. (There is an 'all' option as well, however I do not remember when it was introduced. The (:) works for all versions.)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by