フィルターのクリア

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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by