matlab error using m power - matlab

so if I run this function in matlab
sim1(row,1:512)= ((image(row,1:512,1)-a(1,1)));
it runs fine. now if I modify it to take the square like this
sim1(row,1:512)= ((image(row,1:512,1)-a(1,1)))^2;
it gives me the error, error using ==> mpower
matrix dimensions must agree. Why is this giving me the error, I can do this element by element but I have a lot of data and it will take forever.

It seems you want to do an element by element power which is .^2 not ^2
That is, change to
sim1(row,1:512)= ((image(row,1:512,1)-a(1,1))).^2;

Related

Symbolic Math Toolbox hitting divide by zero error when it used to evaluate to NaN

I've just updated to Matlab 2014a finally. I have loads of scripts that use the Symbolic Math Toolbox that used to work fine, but now hit the following error:
Error using mupadmex
Error in MuPAD command: Division by zero. [_power]
Evaluating: symobj::trysubs
I can't post my actual code here, but here is a simplified example:
syms f x y
f = x/y
results = double(subs(f, {'x','y'}, {1:10,-4:5}))
In my actual script I'm passing two 23x23 grids of values to a complicated function and I don't know in advance which of these values will result in the divide by zero. Everything I can find on Google just tells me not to attempt an evaluation that will result in the divide by zero. Not helpful! I used to get 'inf' (or 'NaN' - I can't specifically remember) for those it could not evaluate that I could easily filter for when I do the next steps on this data.
Does anyone know how to force Matlab 2014a back to that behaviour rather than throwing the error? Or am I doomed to running an older version of Matlab forever or going through the significant pain of changing my approach to this to avoid the divide by zero?
You could define a division which has the behaviour you want, this division function returns inf for division by zero:
mydiv=#(x,y)x/(dirac(y)+y)+dirac(y)
f = mydiv(x,y)
results = double(subs(f, {'x','y'}, {1:10,-4:5}))

Matlab fir1 function error

I'm running Matlab 2014a on Linux and trying to apply a simple FIR filter using the fir1 function. I keep getting the following error, no matter how I try to build the filter:
>>fir1(15,[0.1])
Error using *
Inner matrix dimensions must agree.
>>Error in firls (line 80)
cos_ints = [omega; sin((1:N)' * omega)];
>>Error in fir1 (line 121)
hh = firls(L-1,ff,aa);
I've used the debugger to go to the line of code, and it looks like it's always trying to multiply a column vector of length(order), (1:N)', by another column vector, omega. This doesn't make any sense. Is the fir1 function broken, or am I doing something wrong? This error occurs for me even if I try to run the examples given by MathWorks.
I would guess that Matlab's firls function is masked by another function of the same name, which is in Matlab's path and therefore gets called from fir1.
What do you get when you type :
which firls
? - You should get something which ends in \toolbox\signal\signal\firls.m

Using a value from a matrix as a multiplier in an equation?

Started using MatLab a couple of weeks ago, I don't know much proper syntax / terminology.
I'm trying to use a value in a 3x1 matrix as a multiplier in an equation later.
This is to draw a circle with radius and centre point defined by values input by the user.
I have a pop-up window, the values are input by the user and stored in this 3x1 cell (labelled answer).
How do I use the second value of that matrix, answer(2), in the following equation:
x = 'answer(2)' * cos(theta) + xCentre;
This error message appears:
Error using .*
Matrix dimensions must agree.
Error in Disks (line 40)
x = 'answer(2)'.* cos(theta) + xCentre;
In MATLAB, apostrophes ('') define a string. If the name of your matrix is answer, you can refer to its second value by the command answer(2) as mentioned by #Schorsch. For more infos on vectors and matrices, you can check this site.
In addition to what the previous answer says, its important to understand what exactly you are doing before you do it. Only add the ('') if you are defining a string, which generally occurs when dealing with variables. In your case, you simply have a matrix, which is not a string, but rather a set of numbers. You can simply do answer(2) as aforementioned, because answer(2) calls up the second value in your matrix while 'answer(2)' has you trying to define some variable that does not exist.
the most important thing is truly understanding what you are doing to avoid the basic syntax errors.

MATLAB Inner matrix dimensions must agree

I have t=linspace(1, 10, 91)
I have to define with those values the function y=(((e^(t/10))sin(t))/((t^2)+1)
I write this in MATLAB:
y=((exp(t/10)*sin(t))/((t.^2)+1)
Matlab says:
??? Error using ==> mtimes
Inner matrix dimensions must agree.
I then tried to fix it whatever way possible and put a period before * and this is what I got:
y=((exp(t/10).*sin(t))/((t.^2)+1))
y =
0.0077
I think this isn't the answer because it is not giving me the answer for each value of the matrix. I really don't know what happened.
Can someone help?
Your missing the dot before /:
y=((exp(t/10).*sin(t)) ./ ((t.^2)+1))
Note: You can easily find problems like this on your own. You could have done
((exp(t/10).*sin(t))
and seen that it works as expected. Then you could try ((t.^2)+1)). Wow, that works as well. Thus, the problem has to be cause by the /. From there to ./ it is just a small step.

I am not able to figure out how to carry out the Matrices multiplication in matlab

I have to carry out the following operation
R=[0,0.5,-0.25;-0.25,0,0.25;0,0,0.25];
B=[0,k21,k31;k12,0,k32;0,0,k];
G=inv(R).*B;
g=det(G);
but Matlab is showing the following error
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> g at 60
B=[0,k21,k31;k12,0,k32;0,0,k];
K21,K31,K12,K32 and k all have dimensions of 923334 by 1. Can anyone help me how can I carry out the following operation.
Your code works well for me. Check that the k-values (k12,k31,k32...) are scalars (or 1x1 dimension)
EDIT :
For the case you mention, k's are nx1, one simple way is to perform a loop:
R=[0,0.5,-0.25;-0.25,0,0.25;0,0,0.25];
for ii=1:length(k)
B=[0,k21(ii),k31(ii);k12(ii),0,k32(ii);0,0,k(ii)];
G=inv(R).*B;
g(ii)=det(G);
end
There is also a "vectorized" way to do that, but it seems to be good enough...