- = is used for assignment of values to a variable.
- == is used to test for logical equivalence.
Error in for and if cycles for average
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Stefano Alberti
el 3 de Mayo de 2016
Comentada: Stephen23
el 3 de Mayo de 2016
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
0 comentarios
Respuesta aceptada
Stephen23
el 3 de Mayo de 2016
Editada: Stephen23
el 3 de Mayo de 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 comentarios
Stephen23
el 3 de Mayo de 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:
- use the third output of unique to get a set of indices
- 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Thermal Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!