필터 지우기
필터 지우기

Error in for and if cycles for average

조회 수: 1 (최근 30일)
Stefano Alberti
Stefano Alberti 2016년 5월 3일
댓글: Stephen23 2016년 5월 3일
Hi to all, I have some problems with this code. There is a error on 18th line (if m_misure(j,1)=data_corr) --> 'The expression to the left of the equals sign is not a valid target for an assignment.' Why ? I don't understand. I need a code to read a values series with dates and an average of these for each days (number of values for day is variable).
Thanks,
Stefano
%vettore colonna con data (solo giorno) - importazione
c_giorni=csvread(...)
%vettore colonna con valori - importazione
c_misurazioni=csvread(...)
%definiamo matrice formata dai due valori
m_misure=[c_giorni c_misurazioni];
%definiamo matrice in cui metteremo data e media
m_medie=[];
%definiamo ciclo for
for i=1:length(c_giorni)
if i>1 & m_misure(i,1) ~= m_misure(i-1,1)
somma_valori=0;
numero_valori=0;
data_corr=m_misure(i,1);
for j=1:length(c_giorni)
if m_misure(j,1)=data_corr
somma_valori=somma_valori+m_misure(j, 2);
numero_valori=numero_valori+1;
end
end
m_medie(i,1)=data_corr;
m_medie(i,2)=somma_valori/numero_valori;
end
end

채택된 답변

Stephen23
Stephen23 2016년 5월 3일
편집: Stephen23 2016년 5월 3일
Because you are using the wrong 'equals':
This is why you should learn to read the documentation: it tells us how to use MATLAB. For whatever reason, beginners often seem to be allergic to reading the help, but it is really very easy to read!
In any case, you need to change that line to this:
if m_misure(j,1) == data_corr
^
  댓글 수: 3
Stefano Alberti
Stefano Alberti 2016년 5월 3일
편집: Stefano Alberti 2016년 5월 3일
For real my big problem is the code, its functionality not the error that i found alone....
Stephen23
Stephen23 2016년 5월 3일
Rather than using loops, a much simpler solution for your task is to use accumarray. Have a look at this similar question:
Basically you need to do two steps:
  1. use the third output of unique to get a set of indices
  2. use these in indices in accumarray to collect the data into groups and apply your chosen function.
That's all. Use the internet to find examples.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Thermal Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by