# How to fix "Error using == Arrays have incompatible sizes for this operation." in this code?

13 views (last 30 days)
Chiara Calbucci on 18 Oct 2021
Answered: Rik on 18 Oct 2021
How can i fix this problem?
%Curva di possibilità climatica
T=10; %[anni]
nanni=size(dati,1);
%durate degli eventi registrati
d=[1 3 6 9 12 15 18 21 24 27];
%calcolo parametri della distribuzione di gumbel
media=mean(dati);
sigma=std(dati);
alfa=sigma/(1.645^0.5);
u=media-0.5772*alfa;
%calcolo altezza di pioggia di assegnato tempo di ritorno T
hT=u-alfa*log(-log(1-1/T));
%h=at^n
%1) -->stima dei parmetri con linearizzazione
x=log(d);
y=log(hT);
n1=(sum(x.*y)-length(d)*mean(x)*mean(y))/(sum(x.^2)-length(d)*(mean(x)*mean)^2);
a1=exp(mean(y)-n1*mean(x));
h1=a1*d.^n1;
"Arrays have incompatible sizes for this operation.
Error in primaesercitazione2 (line 21)
n1=(sum(x.*y)-length(d)*mean(x)*mean(y))/(sum(x.^2)-length(d)*(mean(x)*mean)^2);"
Thanks.
Scott MacKenzie on 18 Oct 2021
It might help if you attach the data file dati.txt

Rik on 18 Oct 2021
Let's have a look at the line that results in an error:
n1=(sum(x.*y)-length(d)*mean(x)*mean(y))/(sum(x.^2)-length(d)*(mean(x)*mean)^2);
Can you spot the error? I can't. So you have to split it into smaller parts to see what array sizes you're dealing with.
%x is log(d), d is 1x10, so x is 1x10
%y is log(hT), which is 1xN or Nx1
%d is 1x10
sum(x.*y)-length(d)*mean(x)*mean(y)
So the error might already be here if N is not 10.
The issue might also be with the division. You also use length instead of size or numel, which should be avoided.
Try saving each part as a separate variable to find the exact source of the problem. You also need to load to a struct to make it clear where each variable comes from.
There is also a more fundamental problem: you call mean without an argument.

R2021b

### Community Treasure Hunt

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

Start Hunting!