Help with this problem

1 回表示 (過去 30 日間)
Alexandra Huff
Alexandra Huff 2016 年 8 月 6 日
回答済み: ossai rex 2019 年 5 月 19 日
Hi. I am trying to solve this problem:
Write a function called simple_stats that takes a matrix N as an input and returns the matrix S as the output. S has the same number of rows as N. Each element of the first column of S contains the mean of the corresponding row of N. Similarly, the second column contains the median values; while the third column has the minimums. Finally, each element of the fourth column of S is equal to the maximum value of given row of N.
I wrote
function S = simple_stats(N)
S = [mean(N,1); median(N,2); min(N,[],3); max(N,[],4)]
but this is not working. any suggestions?

採用された回答

Stephen23
Stephen23 2016 年 8 月 6 日
編集済み: Stephen23 2018 年 4 月 23 日
Your dimensions were all messed up: note that , is used to concatenate the columns horizontally (you used ; which concatenates vertically), and you tried to specify the output column as the input argument to each function (but this should simply be the dimension to work along on the input matrix):
function S = simple_stats(N)
S = [mean(N,2),median(N,2),min(N,[],2),max(N,[],2)];
and tested:
>> X = randi(9,7)
X =
8 5 8 1 6 8 7
9 9 2 8 2 7 8
2 9 4 9 7 3 2
9 2 9 7 1 9 5
6 9 8 7 3 1 5
1 9 9 7 1 4 6
3 5 6 4 1 4 7
>> simple_stats(X)
ans =
6.1429 7 1 8
6.4286 8 2 9
5.1429 4 2 9
6 7 1 9
5.5714 6 1 9
5.2857 6 1 9
4.2857 4 1 7
Hint: don't just write code and hope that it works. Write and check it as you write it. Check every line and every command, that it is doing exactly what you need it to do. Beginners often seem to think that a command is doing something, but never even bother to check. You could easily have run those functions (mean, etc) in the command windows with small matrix, and discovered that the output was not as you expected...
Also, learn to read the documentation. That would also have told you that you were using them incorrectly.

その他の回答 (3 件)

fenam sogani
fenam sogani 2017 年 11 月 5 日
function S = simple_stats(N) S = [mean(N,2),median(N,2),min(N,[],2),max(N,[],2)];
what meaning of 2 here
  1 件のコメント
Stephen23
Stephen23 2017 年 11 月 5 日
Did you read the mean, median, min, and max documentation? The documentation clearly explains what all of the inputs mean.

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


Liban Abu
Liban Abu 2018 年 4 月 23 日
編集済み: Stephen23 2018 年 4 月 23 日
one way i approached this is
a = mean(N,2);
b = median(N,2);
c = min(N,[],2);
d= max(N, [],2);
S = [a b c d]
i dont know if this makes sense
  1 件のコメント
Stephen23
Stephen23 2018 年 4 月 23 日
編集済み: Stephen23 2018 年 4 月 23 日
That is what I showed in my answer, just with several intermediate variables rather than on one line: if you like the clarity of this, go for it!

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


ossai rex
ossai rex 2019 年 5 月 19 日
please what those [] and 2 stand for in the command line

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by