- = 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 回表示 (過去 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
0 件のコメント
採用された回答
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 件のコメント
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:
- 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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!