フィルターのクリア

Cannot identify error in line of code

1 回表示 (過去 30 日間)
Stenila  Simon
Stenila Simon 2017 年 9 月 17 日
コメント済み: Stenila Simon 2017 年 9 月 17 日
Hi everyone,
I'm trying to figure out why this line of code is ending up in error whenever I run the code. Is there something small I'm missing maybe? The error is in line 10 by the way.
Thank you!
EDIT: This is the error message:
">> Cnvalues
Index exceeds matrix dimensions.
Error in Cnvalues (line 15)
Cn12 = ((-CpAOA12(i+1)-CpAOA12(i))./2).*((xvec(i+1))-(xvec(i)));"
  1 件のコメント
Stephen23
Stephen23 2017 年 9 月 17 日
@Stenila Simon: please edit your question and do both of these things:
  1. Show the complete error. This means show us all of the red text. Do NOT use a screenshot: screenshots are useless for us.
  2. Show or upload your code. Do NOT use a screenshot: screenshots are useless for us.

サインインしてコメントする。

採用された回答

Jan
Jan 2017 年 9 月 17 日
編集済み: Jan 2017 年 9 月 17 日
You reset sumCn4 in each iteration to 0 and add Cn4. In consequence the result will contain the last element only - if the code does not stop with an error.
The error message tells you, what the problem is. It is required that you show us the message if you want us to help you. We cannot read your mind :-)
I guess, that either CpA0A4 or xvec does not contain 55 elements, such that the index i+1 must fail.
A nicer and faster solution without a loop:
sumCn4 = -2 * sum((CpA0A4(2:end) + CpA0A4(1:end-1)) ./ diff(xvec))
if CpA0A4 and xvec have the same size.
  1 件のコメント
Stenila  Simon
Stenila Simon 2017 年 9 月 17 日
Thank you so much, I changed my index to 1:53 instead and it worked, as well as putting the "sumCn4=0" outside the loop - thank you both for your help!! :)

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 9 月 17 日
Your initialization of sumCn4 should be before the loop.
Your maximum value for i should be at most min( length(cpAOA4) - 1, xvec - 1) so if those are length 54 then you should have "for i = 1 : 53"

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by