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
Walter Roberson
2020년 4월 23일
prob(j) = prod(cellfun(@(r)normpdf(r,sa_k(j),f*r),raw));
EraGum
2020년 4월 23일
Walter Roberson
2020년 4월 23일
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
2020년 4월 24일
[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
2020년 4월 24일
Walter Roberson
2020년 4월 24일
[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
답변 (1개)
John D'Errico
2020년 4월 23일
편집: John D'Errico
2020년 4월 24일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Exploration and Visualization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!