arrayfun and normpdf error
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I keep getting an error when I use arrayfun. It is the first time I am using this function so it is hard for me to find where I am wrong.
The part of my code with which I struggle is:
[num,txt,raw]=xlsread('NACA_data.xlsx',1,'J2:J30');
f=0.025;
sigma=0.1;
% That's not my full code, just listing the most important bits.
sa_k(j)=k(i)*(b_real(j)^x(i))*(t_real(j)^y(i));
for j=1:length(raw)
prob(j)=prod(arrayfun(@(r)normpdf(r,sa_k(j),f*r),raw)); % I get errors here (f*r)
end
I am getting an error "Undefined operator '*' for input arguments of type 'cell' "
I researched that I need to use a cell array for arrayfun, that's why I am using the "raw" data.
I tried to change r from f*r like f*num or f*cell2mat(r) but then I got this error:
"Non-scalar arguments must match in size".
Do you know where the error is? Could you help me to fix it?
7 comentarios
Walter Roberson
el 24 de Abr. de 2020
[num,txt,raw]=xlsread('NACA_data.xlsx',1,'J2:J30');
f=0.025;
sigma=0.1;
% That's not my full code, just listing the most important bits.
sa_k(j)=k(i)*(b_real(j)^x(i))*(t_real(j)^y(i));
for j=1:length(raw)
pd{j} = cellfun(@(r)normpdf(r,sa_k(j),f*r),raw);
prob(j) = prod(pd{j}) ;
end
Respuestas (1)
John D'Errico
el 23 de Abr. de 2020
Editada: John D'Errico
el 24 de Abr. de 2020
It looks like Walter has resolved the issue with an explicit error. However, the other problem of zeros still persists. Almost always this is an issue of numbers that are far too small, so underflows from the normal PDF itself, or products of small numbers, which will then definitely underflow.
However, it should be noted that in a vast amount of the time, these problems can be handled by working with the log of the product. Thus if you want to optimize the product, you can as well optimize the log of the product, since the log function is a monotonic transformation.
In fact the log of the normal PDF is trivial to compute. You really don't even need to use normpdf. And the log of the product is just the sum of the logs.
But first, you need to look at the individual elements of that product. Are they really small? If so, then expect an underfow.
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!