필터 지우기
필터 지우기

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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by