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 view (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 7 Nov 2016
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 Comments
Walter Roberson
Walter Roberson on 7 Nov 2016
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 ?

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Identification 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!