arrayfun and normpdf error
Show older comments
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 Comments
Walter Roberson
on 23 Apr 2020
prob(j) = prod(cellfun(@(r)normpdf(r,sa_k(j),f*r),raw));
EraGum
on 23 Apr 2020
Walter Roberson
on 23 Apr 2020
try removing the prod() call and looking to see whether the entries are all 0, or partly 0, or are just small enough that when you multiply them it underflows to 0.
Walter Roberson
on 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} = arrayfun(@(r)normpdf(r,sa_k(j),f*r),raw);
prob(j) = prod(pd{j}) ;
end
now you can examine the content of the pd cell to see whether individual elements are 0 or all are 0 or they underflow when multiplied together. This is a debugging step.
EraGum
on 24 Apr 2020
Walter Roberson
on 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
Answers (1)
John D'Errico
on 23 Apr 2020
Edited: John D'Errico
on 24 Apr 2020
0 votes
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.
Categories
Find more on Exploration and Visualization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!