Matlab error while finding log - matlab

I am trying to find log of base 10 of each pixel of an image in matlab using following code
m1 = imread('owl','pgm');
for x = 1:size(m1,1)
for y = 1:size(m1,2)
m1(x,y) = log10(m1(x,y));
end
end
here m1 is a 2-D array of order 221 X 201.
but I am facing this error
??? Undefined function or method 'log2' for input arguments of type 'uint8'.
Error in ==> log10 at 20
y = log2(x);
Error in ==> q2 at 38
m1(x,y) = log10(m1(x,y));
but when I debug log function using following code
fprintf('log of 190 is %d', log10(190));
it gives me right output I dont know what happened when I use the same code in the loop.

The error message tells you what the problem is, you've tried to apply the log10 function to a value of type uint8 and the function is not defined for that type of number. What you haven't realised is that imread, when an image file meets certain criteria (read the documentation for what those criteria are) will capture the pixel data into an array of uint8s, not real numbers.
If you want to take the logarithm of a uint8 you'll either have to define a logarithm function of your own which takes such inputs, or, more straightforward, cast the uint8 to a type which log10 is happy with. For example, you could write:
log10(double(m1(x,y)))
And by now you'll have realised why your diagnostic test didn't tell you anything useful, when you execute the command log10(190) Matlab, by default, decides that 190 is of type double and computes the logarithm without complaint. log10(uint8(190)) tells a different story.

Related

Why does MATLAB's norm function work when I manually enter the pixel values, but not when used in accessing the pixel values from the image as below?

MATLAB code included for reference below.
The pixel value displayed is 153 199 215.
I can calculate the norm (330.5072) using the norm function in the MATLAB command line.
However, when I run the code below, upon reaching the norm function, MATLAB errors with "Undefined function 'norm' for input arguments of type 'uint8'".
for i = 1:length(centers)
center = round(centers(i,:));
disp(center);
pixel = rgb(center(1), center(2),:);
pixel = [pixel(1,1,1) pixel(1,1,2) pixel(1,1,3)];
disp(pixel);
n = norm(pixel);
MATLAB's 'norm' function does not take uint8 data as an argument.
Cast pixel to a double using
pixel = double(pixel);
before calling the norm function, and the code runs without error.

Calculating power with ^

I am trying to model a graph as shown in the attached image. The equation for which I am modelling is also shown in the image.
My codings are,
sigmafu=1660;
phi=0.0:0.01:90;
e=2.7183; %I searched on internet to find e value of Euler number and I
%found this.
%Dont know whether MATLAB bydefault has value of e, like MATLAB has value
%pi.
pw= (-0.3)*phi*(180/180);
F=sigmafu*(0)* 2.7183^(pw);
plot (phi,F)
I am getting the following error by using the above codings:
Error using ^. Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead.
Error in myeqsetlin (line 126): F=sigmafu*(0)* 2.7183^(pw);
Can anyone help me correct the code? Also, does MATLAB have the default value of e (Euler's number), if so how can I use it?
You can get the value for e with exp(1). meaning e^1.
sigmafu = 1660;
phi = 0.0:0.01:90;
pw = -0.3*phi*pi/180;
F = sigmafu*exp(pw);
plot(phi,F)

MATLAB: Using FZERO on a function which has a vector output

I am working on my thesis and running in some programming problems in Matlab. I am trying to implement the ''golden Bisection Method'' to speed up my code. To this end, I've consulted the build in function FZERO.
So I am determining the difference between two vectors which are both (1x20).
Difference = Clmax_dist-cl_vec;
Clmax_dist comes from a semi-empirical method and cl_vec comes from the excecution of an external AVL.exe file.
Essentially, this difference depends only on one single variable AOA because the Clmax_dist vector is a constant. Hence, I am constantly feeding a new AOA value to the AVL.exe to obtain a new cl_vec and compare this again to the constant Clmax_dist.
I am iterating this until one of the element in the vector becomes either zero or negative. My loop stops and reveals the final AOA. This is a time consuming method and I wanted to use FZERO to speed this up.
However, the FZERO documentation reveals that it only works on function which has a scalar as input. Hence, my question is: How can I use FZERO with a function which has a vector as an output. Or do i need to do something totally different?
I've tried the following:
[Difference] = obj.DATCOMSPANLOADING(AOA);
fun=#obj.DATCOMSPANLOADING;
AOA_init = [1 20];
AOA_root = fzero(fun,AOA_init,'iter');
this gave me the following error:
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 423)
while fb ~= 0 && a ~= b
Error in CleanCLmax/run (line 11)
AOA_root = fzero(fun,AOA_init,'iter');
Error in InitiatorController/moduleRunner (line 11)
ModuleHandle.run;
Error in InitiatorController/runModule (line 95)
obj.moduleRunner(ModuleHandle);
Error in RunSteps (line 7)
C.runModule('CleanCLmax');
The DATCOMSPANDLOADING function contains the following:
function [Difference] = DATCOMSPANLOADING(obj,AOA)
[Input]= obj.CLmaxInput; % Creates Input structure and airfoil list
obj.writeAirfoils(Input); % Creates airfoil coordinate files in AVL directory
[Clmax_dist,YClmax,Cla_mainsections] = obj.Clmax_spanwise(Input); % Creates spanwise section CLmax with ESDU method
[CLa] = obj.WingLiftCurveSlope(Input,Cla_mainsections); % Wing lift curve slope
[Yle_wing,cl_vec] = obj.AVLspanloading(Input,CLa,AOA); % Creates spanloading with AVL
Difference = Clmax_dist-cl_vec;
end
If I need to elaborate further, feel free to ask. And of course, Thank you very much.
fzero indeed only works on scalars. However, you can turn your criterion into a scalar: You are interested in AOA where any of the elements in the vector becomes zero, in which case you rewrite your objective function to return two output arguments: minDifference, which is min(Difference), and Difference. The first output, minDifference is the minimum of the difference, i.e. what fzero should try to optimize (from your question, I'm assuming all values start positive). The second output you'd use to inspect your difference vector in the end.

What is a good substitute for matlabFunction?

I wrote a small program in MATLAB to compute the Shapley value
using the multi-linear extension of a TU game. However, I run
into trouble with the Symbolic Math Toolbox of MATLAB. In
the program I have to integrate a set of functions to get the
Shapley value. However, inside a MATLAB program I cannot use
the int() command
Error using sym/subsindex (line 663) Ivalid indexing or function definition. When defining a function, ensure that the body of the function is a SYM object. When indexing, the input must be numeric, logical or ':'.
Error in ShapleyValueML (line 65)shv(jj)=int(dfy,0,1)
as a consequence I have to use integral() instead. In this case, I
need to transcribe the set of expressions into MATLAB function handle
with matlabFunction(). However, on all Linux machines (MATLAB R2014a) on
which I have access this command does not work (see the discussion below).
As a workaround, the MATLAB program returns the set of functions
into the current workspace, there the Shapley value can be computed
using the int() command.
To make the discussion more concrete, let us consider this small
MATLAB program first.
function [shv,F,dfm]=ShapleyValueML(v)
N=length(v);
[~, n]=log2(N);
S=1:N;
int=0:-1:1-n;
mat=(rem(floor(S(:)*pow2(int)),2)==1);
cmat=(rem(floor(S(:)*pow2(int)),2)==0);
x=sym('x',[1 n]);
mx=1-x;
y = sym('y');
vy=ones(1,n)*y;
F=0;
shv=zeros(1,n);
dfm=cell(1,n);
for ss=1:N
pd1=x(mat(ss,:));
pd2=mx(cmat(ss,:));
pd=prod(pd1)*prod(pd2)*v(ss);
F=pd+F;
end
F=expand(F);
for jj=1:n
dF=diff(F,x(jj));
dfy=subs(dF,x,vy);
%% Does not work!! MATLAB bug???
% mf=matlabFunction(dfy);
% shv(jj)=integral(mf,0,1);
%%
%% The best would be to use:
%%
% shv(jj)=int(dfy,0,1)
%% but it cannot be used inside a program.
dfm{jj}=dfy;
end
end
The commented parts are the parts that do not work inside
the program, but are needed to compute the Shapley value
with that program, which is its purpose. I tested this program
up to 12 players, and I was able to successfully calculate the
Shapley value by a two step procedure. Hence, the above program
specifies correctly the considered problem. To get a better
understanding of this two step procedure and of the functionality
of the above program, let us focus on a three person game.
The values of the coalitions are given by the following data array
>> v = [0,0,90,0,100,120,220];
Notice that coalitions are ordered in accordance with their unique
integer representations. The game is defined, we can now evaluate
the multi-linear extension and the set of partial derivatives with
the above program, but not the Shapley value.
>> [shv,F,dfm]=ShapleyValueML(v);
Integration of the set of partial derivatives runs over the diagonal
of the unit-cube, but then we can set the variables from [x1,x2,x3]
to [y,y,y], and integration runs from 0 to 1.
>> for k=1:3, shv(k)=int(dfm{k},0,1);end;
The solution of the integration is the Shapley value given by:
>> shv
shv =
65 75 80
Checking that this is indeed the Shapley value can be accomplished
with a potential function approach implemented in
>> sh_v=ShapleyValue(v)
sh_v =
65 75 80
that ships with my MATLAB Game Theory Toolbox MatTuGames from
http://www.mathworks.com/matlabcentral/fileexchange/35933-mattugames
Instead of integrating with int() one can also use integral(),
but then the contents like
>> dfm{1}
ans =
- 90*y^2 + 190*y
must be rewritten with matlabFunction() into a function handle. As I
have mentioned above this does not work under Linux
(MATLAB R2013a,R2013b,R2014a). To see this let us try to reproduce
the example
>> syms x y
>> r = sqrt(x^2 + y^2);
from the documentation at the URL:
http://www.mathworks.de/de/help/symbolic/generate-matlab-functions.html?searchHighlight=matlabFunction
This should give
ht =
#(x,y)tanh(sqrt(x.^2+y.^2))
but I get
>> ht = matlabFunction(tanh(r))
Cell contents reference from a non-cell array object.
Error in vectorize (line 15)
c = cells{i};
Error in sym/matlabFunction>mup2mat (line 319)
res = vectorize(res(2:end-1)); % remove quotes
Error in sym/matlabFunction>mup2matcell (line 304)
r = mup2mat(c{1});
Error in sym/matlabFunction (line 123)
body = mup2matcell(funs);
Here comes now my question: Exists there an alternative procedure to
get from
>> dfm{1}
ans =
- 90*y^2 + 190*y
a function handle
>> df=#(y) (- 90.*y.^2 + 190.*y)
df =
#(y)(-90.*y.^2+190.*y)
to integrate it by
>> integral(df,0,1)
ans =
65
Or to put it differently. Is there an alternative method available to
change multiplication * to element-wise multiplication .*, and the
power operation ^ to element-wise power.^?
Of course, any suggestions of improvement for the above MATLAB program
are highly appreciated.
I think I know what the problem is; Towards the beginning of ShapleyValueML function, you have a variable named int which shadows the builtin integration function:
...
int=0:-1:1-n; %# <-- problem!
...
shv(jj)=int(dfy,0,1)
...
That explains the error coming from sym/subsindex, you were using a symbolic object as an index into the numeric array int.
Change the variable name to something else, and the commented code runs fine (the symbolic integration)! Simple as that :)

neural network,matlab programming

what can i do to solve this error in my program(learning the net)? it appear after sim. the net.
Error in ==> network.sim>simargs at 236
switch class(P)
??? Output argument "Pi" (and maybe others) not assigned during call to "C:\Program
Files\MATLAB\R2008b\toolbox\nnet\nnet\#network\sim.m>simargs".
Error in ==> network.sim at 173
case 2, [err,X,Xi,Ai,T,Q,TS,matrixForm] = simargs(net,X);
Error in ==> mlptrptest at 62
y = sim(net,A{1,1})
Note: Don't fall into the same trap I did. There is also a function called SIM in Simulink that will likely show up in searches for the function SIM in the Neural Network Toolbox...
The first thing I would check is that the second input argument A{1,1} is of the correct form. Specifically, A{1,1} would have to be a cell array or a matrix of doubles. If it is of any other form, like a structure or a matrix of any other class, you will get the error you are seeing. Admittedly, this particular error isn't handled very well by the subfunction simargs, in that it gives you some cryptic messages that don't really tell you the basic problem, which is that your input is not in the right format.
Here are a couple things to check:
Do you really mean to pass the first element of cell array A as an input argument, or do you mean to simply pass the cell array itself? If it's the second option, just do this:
y = sim(net,A);
If you do mean to pass the first element of A as an input argument, double-check it's class using the function CLASS:
class(A{1,1})
If you don't see double displayed, it means the first element of A is not the right type. If it is a matrix, you can convert it to double before you pass it to SIM like so:
y = sim(net,double(A{1,1}));