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...
0 个评论
回答(2 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!