arrayfun and normpdf error

2 Ansichten (letzte 30 Tage)
EraGum
EraGum am 23 Apr. 2020
Kommentiert: Walter Roberson am 24 Apr. 2020
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 Kommentare
EraGum
EraGum am 24 Apr. 2020
Thanks for a quick response. I did as you said but now I am getting this error: "Undefined operator '*' for input arguments of type 'cell' " and nothing works when I am trying to fix it
Walter Roberson
Walter Roberson am 24 Apr. 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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

John D'Errico
John D'Errico am 23 Apr. 2020
Bearbeitet: John D'Errico am 24 Apr. 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.

Community Treasure Hunt

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

Start Hunting!

Translated by