What does mean(s(:)) do and the reason for using " : " for mean of a square matrix?
조회 수: 1 (최근 30일)
이전 댓글 표시
SRAVAN KUMAR REDDY METTUPALLI
2017년 9월 1일
댓글: SRAVAN KUMAR REDDY METTUPALLI
2017년 9월 3일
The working of the below mentioned and the reason for using " : " avg = mean(s(:));
댓글 수: 0
채택된 답변
John D'Errico
2017년 9월 1일
Well, what does mean do, when applied to a matrix? TRY IT!!!!!!!!! You will learn by trying things.
magic(3)
ans =
8 1 6
3 5 7
4 9 2
>> mean(magic(3))
ans =
5 5 5
>> mean(magic(3),2)
ans =
5
5
5
So when you apply mean to a MATRIX, it finds the mean across rows, or down columns.
When you apply mean to a vector, it finds the mean of the vector elements.
mean(1:10)
ans =
5.5
So how can you find the mean of an entire matrix?
Can you convert a matrix to a vector? If so, then you can just use mean.
What does s(:) do? It converts a matrix to a column vector. Therefore, you should now know what mean(s:)) does, and why it works.
When you don't understand some code in MATLAB, take it apart, one level at a time. Think what each part does. And if you don't know how something works, EXPERIMENT!
추가 답변 (1개)
Adam
2017년 9월 1일
편집: Adam
2017년 9월 1일
(:) reshapes the array to a column vector, as described in
doc colon
This is done when taking the mean of a 2d image as a simple way to avoid having to do
mean( mean( s ) )
In the case of mean and some other functions there is a special builtin function
doc mean2
but I always use the colon notation personally.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!