trouble using "mean" function in MATLAB?

3 次查看(过去 30 天)
consider A=[1 2 3] if i use mean(A(1):A(3)) it gives 2 which is the mean of first element and third element of A.
if A=[3 2 1],and if i use mean(A(1):A(3)) then it says NaN. Why should this occur?shouldnt the command just give the mean of the first and third digit in the array?Any help will be appreciated...

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2012-10-23
Maybe you want
mean(A(1:3))

Matt Fig
Matt Fig 2012-10-23
You are creating a vector with the elements of A, rather than indexing into A with a vector. Look at what happens:
A = [1 2 3];
B = A(1):A(3) % Same as:
B2 = 1:3 % Read: make a vector from 1 to 3 in steps of 1
isequal(B,B2)
A = [3 2 1];
B = A(1):A(3) % Same as:
B2 = 3:1 % Read: make a vector from 3 to 1 in steps of 1
isequal(B,B2)
Probably what you want to do is index into A with a vector:
A(1:3) % Read: take elements 1 through 3 of A.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by