Here's two ways to enter the command in Matlab. I don't think there's any difference between them. However, the result is really different. So I wonder what's I missed in this situation.
Here's the first input:
>> A = [(-0.025+0.01i) -0.025;
3 (1-2i)];
>> B = [(5.7955+1.5529i) 0]';
>> I=inv(A)*B
The output is like this:
I =
1.0e+02 *
-0.7063 - 1.2723i
-1.1030 + 1.6109i
Here's the second input:
>> A = [(-0.025+0.01i) -0.025;3 (1-2i)];
>> B = [(5.7955+1.5529i);0];
>> I=inv(A)*B
And the Matlab give me the result below:
I =
2.44764705882354 - 145.499411764706i
-176.067882352941 + 84.3624705882353i
I'm really confused about this situation. If you know anything please let me know about it. Thanks.
Use B = [(5.7955+1.5529i) 0].' which is actually element-wise transpose and not B = [(5.7955+1.5529i) 0]' which is conjugate transpose.
One can also use an explicit call to transpose command - B = transpose([(5.7955+1.5529i) 0])
Related
I am working on a MATLAB code so that I can basically do this
To accomlish that, my code looks like this
A = [1:30]'; % Example matrix
rows = 3;
for i=1:(numel(A)-rows+1)
B(1:rows,i)=A(i:i+rows-1,1);
end
Can someone help me do the same in a simpler way? I am guessing there is a one-line command that can solve that (maybe I'm wrong).
Let A and rows be defined as in your code. I'm assuming the values in A are just an example. If they are always 1, 2, ..., some of the solutions below can be simplified.
A = [1:30].';
rows = 3;
Here are some approaches:
My choice:
B = A(bsxfun(#plus, (1:rows).', 0:numel(A)-rows));
An alternative:
B = conv2(A.', flip(eye(rows)));
B = B(:, rows:end-rows+1);
Slightly more inefficient:
B = hankel(A);
B = B(1:rows, 1:numel(A)-rows+1);
If you have the Image Processing Toolbox:
B = im2col(A, [rows 1], 'sliding');
I got two sparse matrices A and B, which have identical sparsity pattern (all nonzeros are at exactly the same locations):
i = randi(1000,[50,1]);
j = randi(1000,[50,1]);
a = rand(50,1);
b = rand(50,1);
A = sparse(i,j,a,1000,1000);
B = sparse(i,j,b,1000,1000);
I would like to calculate efficiently exp(A-B) only for the nonzeros, and save it back into A.
I tried to use spfun for that task:
f = #(x,y) exp(x-y);
A = spfun(f,A,B);
but I got an error in spfun saying: "Too many input arguments."
Can anyone suggest an efficient way to calculate it?
It should be calculated many times.
Thanks!
Edit: mikkola suggested A = spfun(#f,A-B) which solves the problem, but the question remains how to do it with a function of two variables that can't be solved using the same trick. For example:
g = #(x,y) x.*cos(y);
A = spfun(#g,A,B);
You can't use
A = spfun(#exp, A-B);
because for entries where A an B are equal you will get 0 instead of 1.
To solve that, you can compute the vector of exponentials at the nonzero components, and then build a sparse matrix from that:
A = sparse(i,j,exp(nonzeros(A)-nonzeros(B))); %// if you have i, j stored
or
A(find(A)) = exp(nonzeros(A)-nonzeros(B));
Edit
According to the documentation, spfun can take only two inputs: a function and one sparse matrix.
So you cannot do directly what you want to do. The best solution is probably what has been suggested in the comments, i.e.:
res = spfun(#exp, A-B);
Best,
output is a 3d matrix with size(output) == [height width N] and input is a 2d matrix with size(input) == [height width] . I need to implement the following code in one line.
for k = 1:size(output,3)
f = output(:,:,k);
i_zero = (f==0);
f(is_zero) = input(is_zero);
output(:,:,k) = f;
end
bsxfun approach -
output = bsxfun(#times,output==0,input) + output
Alternative approach -
output = (output==0).*input(:,:,ones(1,N))+ output
I hope the "I need to implement" is not a homework.
Here goes a solution that should solve your problem although not in one line.
new_input=repmat(input,1,1,size(output,3));
output(output==0)=new_input(output==0);
All the answers solve the problem when there is a exact comparison to 0 (as OP required) but for the sake of generalization if you intend to change for another comparison be aware that not all methods work in the same way.
Example below:
CODE:
%Simulation
output=rand(10,10,3);
input=rand(10,10);
% output=randi(9,10,10,3);
% input=randi(9,10,10);
%OP code
output2=[]
for k = 1:size(output,3)
f = output(:,:,k);
i_zero = (f<0.5);
f(i_zero) = input(i_zero);
output2(:,:,k) = f;
end
%repmat code
output3=output;
new_input=repmat(input,1,1,size(output,3));
output3(output<0.5)=new_input(output<0.5);
any(output2(:)-output3(:))
%bsxfun code
output4 = bsxfun(#times,output<0.5,input) + output;
any(output2(:)-output4(:))
%other variation code
output5 = (output<0.5).*input(:,:,ones(1,size(output,3)))+ output;
any(output2(:)-output5(:))
% bultin code
output6=output;
output6(output<0.5)=builtin('_paren',repmat(input,[1,1,size(output,3)]),output<0.5);
any(output2(:)-output6(:))
'-----'
any(abs(output2(:)-output3(:))>eps)
any(abs(output2(:)-output4(:))>eps)
any(abs(output2(:)-output5(:))>eps)
any(abs(output2(:)-output6(:))>eps)
'-----'
sum(abs(output2(:)-output3(:)))
sum(abs(output2(:)-output4(:)))
sum(abs(output2(:)-output5(:)))
sum(abs(output2(:)-output6(:)))
OUTPUT:
ans =
0
ans =
1
ans =
1
ans =
0
-----
ans =
0
ans =
1
ans =
1
ans =
0
-----
ans =
0
ans =
150.5088
ans =
150.5088
ans =
0
If you insist on a single line solution, you can use the (:) operator along with the mod command:
output(output(:)==0) = input(mod(find(output(:)==0)-1,height*width)+1)
where the -1 and +1 are in order to avoid index 0
Here is in a one liner... but uses the undocumented builtin('_paren',... to subscript reference to the output of a function
output(output==0)=builtin('_paren',repmat(input,[1,1,N]),output==0)
without the the undocumented builtin this method gets messy if you want it in one line...
output=subsasgn(output,struct('type','()','subs',{{output==0}}),...
subsref(repmat(input,[1,1,N]),struct('type','()','subs',{{output==0}})))
...sadly I forgot using masks and adding two matrices together was an option...
You should have the 'cat' function:
http://www.mathworks.nl/help/matlab/ref/cat.html
cat(3,matrix1,matrix2,...) to concatenate along the 3rd dimension.
I have the following anonymous function:
f = #(x)x^2+2*x+1
I'm using this so that I use it in the following way:
f(0) = 1
But what if I want to find the derivative of such a function while still keeping it's anonymous function capability? I've tried doing the following but it doesn't work:
f1 = #(x)diff(f(x))
but this just returns
[]
Any thoughts on how to accomplish this?
Of course I could manually do this in 3 seconds but that's not the point...
If you have symbolic math toolbox, you can use symbolic functions to achieve the desired as follows:
syms x
myFun=x^2+2*x+1;
f=symfun(myFun,x);
f1=symfun(diff(f),x);
%Check the values
f(2)
f1(2)
You should get 9 and 6 as answers.
When you do diff of a vector of n elements it just outputs another vector of n-1 elements with the consecutive differences.. so when you put a 1 element vector you get an empty one.
A way to go would be to decide an epsilon and use the Newton's difference quotient:
epsilon = 1e-10;
f = #(x) x^2+2*x+1;
f1 = #(x) (f(x+epsilon) - f(x)) / epsilon;
or just do the math and write down the formula:
f1 = #(x) 2*x+2;
http://en.wikipedia.org/wiki/Numerical_differentiation
#jollypianoman this works to me. Actually you need to say that the symfun has to be evaluate using eval command, then you get all the features of an anonymous function. the best is to read the example below...
clear
N0=1;N1=5;
N=#(t) N0+N1*sin(t);
syms t
Ndot=symfun(diff(N(t)),t);
Ndot_t=#(t) eval(Ndot);
Ndot_t(0)
ans = 5
Ndot_t(1)
ans = 2.7015
[tstop] = fsolve(Ndot_t,pi/3)
tstop =
1.5708
cheers,
AP
I've read up on fsolve and solve, and tried various methods of curve fitting/regression but I feel I need a bit of guidance here before I spend more time trying to make something work that might be the wrong approach.
I have a series of equations I am trying to fit to a data set (x) separately:
for example:
(a+b*c)*d = x
a*(1+b*c)*d = x
x = 1.9248
3.0137
4.0855
5.0097
5.7226
6.2064
6.4655
6.5108
6.3543
6.0065
c= 0.0200
0.2200
0.4200
0.6200
0.8200
1.0200
1.2200
1.4200
1.6200
1.8200
d = 1.2849
2.2245
3.6431
5.6553
8.3327
11.6542
15.4421
19.2852
22.4525
23.8003
I know c, d and x - they are observations. My unknowns are a and b, and should be constant.
I could do it manually for each x observation but there must be an automatic and far superior way or at least another approach.
Very grateful if I could receive some guidance. Thanks for the time!
Given your two example equations; let y=x./d, then
y = a+b*c
y = a+a*b*c
The first case is just a line, for which you can obtain a least squares fit (values for a and b) with polyfit(). In the second case, you can just say k=a*b (since these are both fitted anyway), then rewrite it as:
y = a+k*c
Which is exactly the same line as the first problem, except now b = k/a. In fact, b=b1/a is the solution to the second problem where b1 is the fit from the first problem. In short, to solve them both, you need one call to polyfit() and a couple of divisions.
Will that work for you?
I see two different equations to fit here. To spell out the code:
For (a+b*c)*d = x
p = polyfit(c, x./d, 1);
a = p(2);
b = p(1);
For a*(1+b*c)*d = x
p = polyfit(c, x./d, 1);
a = p(2);
b = p(1) / a;
No need for polyfit; this is just a linear least squares problem, which is best solved with MATLAB's slash operator:
>> ab = [ones(size(c)) c] \ (x./d)
ans =
1.411437211703194e+000 % 'a'
-7.329687661579296e-001 % 'b'
Faster, cleaner, more educative :)
And, as Emmet already said, your second equation is nothing more than a different form of your first equation, the difference being that the b in your first equation, is equal to a*b in your second one.