Clear Filters
Clear Filters

Error in for and if cycles for average

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

Accepted Answer

Stephen23
Stephen23 on 3 May 2016
Edited: Stephen23 on 3 May 2016
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 Comments
Stefano Alberti
Stefano Alberti on 3 May 2016
Edited: Stefano Alberti on 3 May 2016
For real my big problem is the code, its functionality not the error that i found alone....
Stephen23
Stephen23 on 3 May 2016
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Thermal Analysis 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!