I have been looking at this code for a while now, and cannot figure out why matlab is not calculating correctly. Does anyone see anything that I may be doing wrong with this code?
((1-EU_P2par3(:,1))*US_P2par3(:,1))+((1-EU_P2par3(:,2))*US_P2par3(:,2))+((1-EU_P2par3(:,3))*US_P2par3(:,3))+((1-EU_P2par3(:,4))*US_P2par3(:,4))+((1-EU_P2par3(:,5))*US_P2par3(:,5))+((1-EU_P2par3(:,6)*US_P2par3(:,6)))+((1-EU_P2par3(:,7))*US_P2par3(:,7))
Thanks for all the help!
In cases like this, good code formatting is your friend. Using an ellipsis (i.e. ..., the line continuation symbol) to create a multi-line statement can help greatly...
It looks like you have a parenthesis in the wrong place. Your code looks like this:
result = ((1-EU_P2par3(:,1))*US_P2par3(:,1))+...
((1-EU_P2par3(:,2))*US_P2par3(:,2))+...
((1-EU_P2par3(:,3))*US_P2par3(:,3))+...
((1-EU_P2par3(:,4))*US_P2par3(:,4))+...
((1-EU_P2par3(:,5))*US_P2par3(:,5))+...
((1-EU_P2par3(:,6)*US_P2par3(:,6)))+... %# Notice something here?
((1-EU_P2par3(:,7))*US_P2par3(:,7));
And you probably want this:
result = ((1-EU_P2par3(:,1))*US_P2par3(:,1))+...
((1-EU_P2par3(:,2))*US_P2par3(:,2))+...
((1-EU_P2par3(:,3))*US_P2par3(:,3))+...
((1-EU_P2par3(:,4))*US_P2par3(:,4))+...
((1-EU_P2par3(:,5))*US_P2par3(:,5))+...
((1-EU_P2par3(:,6))*US_P2par3(:,6))+... %# Notice the change?
((1-EU_P2par3(:,7))*US_P2par3(:,7));
EDIT:
In addition, as Darren mentions in his answer, you will likely have to use the element-wise multiplication operator .* instead of the matrix multiplication operator *. Explanations of the arithmetic operators can be found here.
Also, your calculation can be greatly simplified by vectorizing it using the function SUM, like so:
result = sum((1-EU_P2par3(:,1:7)).*US_P2par3(:,1:7),2);
Try the following example.
xy = rand(10,2);
a = xy(:,1)*xy(:,2);
% ??? Error using ==> mtimes
% Inner matrix dimensions must agree.
a = xy(:,1).*xy(:,2);
The error arises when you attempt to multiply vectors together. You must use the .* operator to get element-wise multiplication
Hope that helps
Related
the variable nowT is a constant but I don't know how to fix it, it need to be a matrix
My code is:
lambda1=1.064; % unit:um
lambda2=0.532; % unit:um
T=0:500;
nowT=(4.9130+(0.1173+T.*T.*1.65e-8)/(lambda1.*lambda1-(0.212+T.*T.*2.7e-8).^2)-lambda1.*lambda1.*2.78e-2);
ne2wT=(4.5567+T.*T.*2.605E-7+(0.097+T.*T.*2.7E-8)/(lambda2.*lambda2-(0.201+T.*T.*5.4e-8).^2)-2.24E-2.*lambda2.*lambda2);
figure('name','temperature phase matching chart','NumberTitle','off')
plot(T,nowT,T,ne2wT);
Would appreciate some help
I guess that you want element-wise division in nowT, like this:
nowT=(4.9130+(0.1173+T.*T.*1.65e-8)./ ...
(lambda1.*lambda1-(0.212+T.*T.*2.7e-8).^2)-lambda1.*lambda1.*2.78e-2);
The change is simply ...1.65e-8)./(lambda1... instead of ...1.65e-8)/(lambda1...
That code gives the following figure in Octave Online.
I am trying to sum each digit in the number 2^1000, for instance, the sum of each digits in 25346 = 2+5+3+4+6 = 20.
I wrote a code in Matlab looking like this:
clc, clear all, close all,
x=2^1000;
x=vpa(x,400);
sum=0;
while x>0
num=mod(x,10);
sum = sum+num;
x=floor(x/10);
end
sum % = 1349, correct answer should be 1366
For smaller numbers, this code works. I'm guessing there's something fishy with the vpa command, but I can't figure out what.
Anyone have any ideas? Is there an easier and faster way of doing this in matlab?
Merge these two lines as it is shown in the documentation:
x=2^1000;
x=vpa(x,400);
to this
x=vpa(2^1000,400);
Trying to find a way to call the exponentiation function ( ^ ) used in a custom function for every item in a matrix in GNU Octave.
I am quite a beginner, and I suppose that this is very simple, but I can't get it to work.
The code looks like this:
function result = the_function(the_val)
result = (the_val - 5) ^ 2
endfunction
I have tried to call it like this:
>> A = [1,2,3];
>> the_function(A);
>> arrayfun(#the_function, A);
>> A .#the_function 2;
None of these have worked (the last one I believe is simply not correct syntax), throwing the error:
error: for A^b, A must be a square matrix
This, I guess, means it is trying to square the matrix, not the elements inside of it.
How should I do this?
Thanks very much!
It is correct to call the function as the_function(A), but you have to make sure the function can handle a vector input. As you say, (the_val - 5)^2 tries to square the matrix (and it thus gives an error if the_val is not square). To compute an element-wise power you use .^ instead of ^.
So: in the definition of your function, you need to change
result = (the_val-5)^2;
to
result = (the_val-5).^2;
As an additional note, since your code as it stands does work with scalar inputs, you could also use the arrayfun approach. The correct syntax would be (remove the #):
arrayfun(the_function, A)
However, using arrayfun is usually slower than defining your function such that it works directly with vector inputs (or "vectorizing" it). So, whenever possible, vectorize your function. That's what my .^suggestion above does.
Right, I'm really pretty new to the concept of vectorization but I'm trying to get head round it. Currently, I'm trying to adapt some of the code that I wrote to implement canny edge detection into a vectorized form and what I don't understand is why this:
for r=1:fsize
for c=1:fsize
mask(r,c) = mask(r,c)/Z;
end
end
produces a different result to this:
mask(r:fsize,c:fsize) = mask(r:fsize,c:fsize)/Z;
When my understanding is that they should do the same thing?
What is r, what is c in the second solution? Probably you need element-wise division ./:
mask = mask./Z;
If this does not solve your problem, please provide input data to reproduce.
for r=1:fsize
for c=1:fsize
mask(r,c) = mask(r,c)/Z;
end
end
Is equivalent to
mask(1:fsize, 1:fsize) = mask(1:fsize, 1:fsize) / Z;
Note - 1:fsize not c:fsize.
This is assuming that Z is a constant. It would be marginally faster to do * (1 / Z) - doing the division just once, and then multiplying...
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.