配列の各要素同士を足すには?
Show older comments
配列n[i]が(51×1)の配列の時に、 n[1],,,n[20]、 n[21],,,n[40]、・・・ のようにn[i]を20個ずつ各要素を足し、その結果の配列を水平連結させたいです。
下記プログラムで、n[i]にはデータが正常に入っていることは確認できており、display(N)でNの値を見ると、 N = NaN NaN NaN......... と表示されます。 どこが間違っているでしょうか。
理解し辛い質問で申し訳ございません。
よろしくお願い致します。
N=0;
%n[1]からn[400]までのデータがある
for i=1:400
%n[i]を20個分各要素足す
N = N + n[i];
display(N)
%20個ずつ足した結果NをDataに代入
if i == 20
Data = N;
N = 0;
end
if rem(i,20) == 0 && i>= 40
%配列を水平に連結
Data = horzcat(Data,N);
N = 0;
end
end
1 Comment
michio
on 25 Oct 2017
N = N + n[i];
を
N = N + n(i);
に変えて実行したところ、正しく計算されている模様です。 ちなみに n のサイズは必ずしも 20 の倍数ではない場合もありますか?
Accepted Answer
More Answers (0)
Categories
Find more on コマンドの入力 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!