フィルターのクリア

for loop iteration dimension

2 ビュー (過去 30 日間)
NEG
NEG 2015 年 11 月 19 日
編集済み: Shashvat Prakash 2015 年 12 月 16 日
w=[1 2 3 4 5 6 7];
I want to subtract each element from the neighboring one. like to have finally; w2=[1-2,2-3,3-4,4-5,5-6,6-7]; in a for loop i can write i=1 for i=1:7; W2(i)=w(i)-w(i+1)
i=i+1; end but i get error that the last number can be read since it is out of dimension. how should i do then?
so many thanks

回答 (1 件)

Shashvat Prakash
Shashvat Prakash 2015 年 12 月 16 日
編集済み: Shashvat Prakash 2015 年 12 月 16 日
Pay attention to the size of w2. It is a six-element array, not a seven element one. So your code should be:
for i=1:6
w2(i)=w(i)-w(i+1);
end
Alternatively, you can write:
w2=w(1:end-1)-w(2:end)
This avoids the time-consuming loop.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by