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?
0 个评论
采纳的回答
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
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 ?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!