필터 지우기
필터 지우기

there is always zero elements

조회 수: 1 (최근 30일)
MD
MD 2019년 6월 10일
댓글: Star Strider 2019년 6월 10일
Hi. right now i am tryign to learn descriptive statistics and produce them in matlab environment.
let us consider,
c = [ 1 2 3 4 5 6 7 8]
for i=1:2:length(c)
m(i)=(c(i)+c(i+1))/2;
end
disp(m)
But there is always zero elements in m. Why is this happening? how can i get m without any zero element?
Please if there is anyone to help.
Thanks in advance.

채택된 답변

Star Strider
Star Strider 2019년 6월 10일
The reason is that your ‘i’ index skips the even-numbered elements, so the even-numbered elements are set to 0.
The easiest way to avoid that is to just use a separate counter:
c = [ 1 2 3 4 5 6 7 8]
k = 1;
for i=1:2:length(c)
m(k)=(c(i)+c(i+1))/2;
k = k + 1;
end
disp(m)
  댓글 수: 1
Star Strider
Star Strider 2019년 6월 10일
Actually, since you want to take the mean of adjacent pairs of elements, rather than adjacent elements, using the reshape function on your vector, and then taking the mean of the resulting matrix is likely most efficient:
m = mean(reshape(c(:), 2, []))
The result is the same.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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