arrayfun and normpdf error

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

prob(j) = prod(cellfun(@(r)normpdf(r,sa_k(j),f*r),raw));
Thanks, I used this code. The code runs now and does not give any errors.
However, the probability is always prob=0
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.
EraGum
EraGum on 24 Apr 2020
Edited: EraGum on 24 Apr 2020
If I understood you right, I removed prod() call and got this:
prob(j)=cellfun(@(r)normpdf(r,sa_k(j),f*r),raw);
but then there is an error again:
"Unable to perform assignment because the indices on the left side are not compatible with the size of the right side".
I am trying to make this code to multiply normpdf 29 times using the defined rows of data from excel spreadsheet.
P.S. It should not underflow to 0
[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.
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
[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

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 23 Apr 2020
Edited: John D'Errico on 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.

Asked:

on 23 Apr 2020

Commented:

on 24 Apr 2020

Community Treasure Hunt

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

Start Hunting!