Error in for and if cycles for average

1 次查看(过去 30 天)
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