フィルターのクリア

I have "NewCell" that's a cell with 1 row and 6 columns; in which column, there is another cell with 1 column and "k" rows. If the row "k" in NewCell (1,1) is 0, I want to know the average from values in another file (Preos2013S1)

1 回表示 (過去 30 日間)
sum=0;
i=0;
for k=0:size(Preos2013S1)
if NewCell{1,1} == 0
sum=sum+Preos2013S1(k,6);
i=i+1;
end
end
mediacons_0=sum/i;
histogram(mediacons_0)
It does not work because Undefined operator '==' for input arguments of type 'cell'. What do I need to change?

採用された回答

Walter Roberson
Walter Roberson 2016 年 11 月 7 日
It is firmly recommended that you do not use sum as the name of a variable, as that interferes with using the important sum function. That tends to lead to bugs, and confuses other people reading the code.
You should never use size() with a single parameter as one of the input parameters to the colon operator (":") because the colon operator is only defined when the parameters are scalars but size() with only a single parameter always returns a function. You should either use length() or use size() with two inputs to indicate which dimension you are taking the size of.
You are iterating over a size, but you are starting from 0 instead of 1. That is likely to lead you to either indexing at 0 or indexing out of bounds.
Your test should be
if NewCell{1,1}{k} == 0
  3 件のコメント
Walter Roberson
Walter Roberson 2016 年 11 月 7 日
As I wrote above,
"You are iterating over a size, but you are starting from 0 instead of 1. That is likely to lead you to either indexing at 0 or indexing out of bounds."
0 is not a valid index in MATLAB; why are you trying to use it to index Preos2013S1 ?
Eduardo Rocha
Eduardo Rocha 2016 年 11 月 7 日
Sorry for not noticing that part of your answer. Thank you very much, once more!

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

その他の回答 (0 件)

カテゴリ

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