I tried to find a function of matlab, I found 'tf' but I didn't know how to use it :/
So I am trying to write a code of transition matrix, from:
mat1=[1,1,1;
1,1,0;
1,0,0];
to this one:
mat2=[1,2,3;
0,1,1;
0,0,1]
I think I have to do something like:
a{1} * mat2(1,:) + a{2} * mat2(1,:) + a{3} * mat2(1,:) = mat1(1,:);
a{4} * mat2(2,:) + a{5} * mat2(2,:) + a{6} * mat2(2,:) = mat1(2,:);
a{7} * mat2(3,:) + a{8} * mat2(3,:) + a{9} * mat2(3,:) = mat1(3,:);
find the a{1}, a{2}, .... a{9} that solve these equations, and put it in the columns:
result = [a{1} a{4} a{7};
a{2} a{5} a{8};
a{3} a{6} a{9}];
Is my way good? can someone tell me please how to use the matlab function for creating a transition matrix for my matrices?
This is an example:
1(1,2,3)-1(0,1,1)-1(0,0,1) = (1,1,1)
1(1,2,3)-1(0,1,1)-2(0,0,1) = (1,1,0)
1(1,2,3)-2(0,1,1)-1(0,0,1) = (1,0,0)
then, the result should be:
result = [1 1 1
-1 -1 -2
-1 -2 -1]
now if I take the vector (3, -1, -1) in the basis of B, I got (1,0,0) in the basis of c.
The tf function computes the transfer function model. That does not seem to be in any way related to your problem.
EDITED:
Now I got it, so the result matrix R that you want is actually
R = (M1 * M2^-1)^T
hence
result = (mat1 * inv(mat2))';
where the transposition is simply due to your choice of picking the indices column-first.
However, I must underline that this solution yields
mat1 = result' * mat2;
so R^T is not the transition matrix from M1 to M2, but the transition matrix from M2 to M1.
TF() is transfer function. As in controls. For example, if the function is F(s) = (1/5s^2+2s+1) Numerator = [1] Denominator = [5 2 1] and therefore your transfer function F = tf([1], [5 2 2]). From here you can do a lot of fun engineering stuff like bode(F) and so on.
What I think you are trying to do is: http://www.mathworks.com/matlabcentral/newsreader/view_thread/132415
Related
I am trying to understand now fminunc (fmincon) works, however I keep getting error.
When I use documentation example with two variables
fun = #(x)3*x(1)^2 + 2*x(1)*x(2) + x(2)^2 - 4*x(1) + 5*x(2);
x0 = [1,1];
[x,fval] = fminunc(fun,x0);
everything works fine.
Hovewer, when I am trying to fit a plane for 3 points,
the code does not work
n0 = [ 0 1 -2;
1 2 1;
-2 -4 -4]
fun = #(x) [x(1) x(2) x(3)] * n0 - [1 1 1]
The task for fminunc is just an example. I know I can solve it easily analytically.
The cost function returns a scalar. What you have written returns a [1x3] matrix. You could try something like this if you want to minimise the euclidean distance
fun = #(x) sum(([x(1) x(2) x(3)] * n0 - [1 1 1]).^2);
If I have a matrix A and I want to evaluate x' * A * x for multiple values of x, how can I vectorize this?
(I could do X' * A * X and take the diagonal, but this is clearly inefficient.)
One way to think about it is that you are trying to take a bunch of dot products between the vectors in X and the vectors in AX. Matlab has a function for that:
N = 10; % number of x's
M = 100; % length of x's
X = rand(M,N);
A = rand(M, M);
% way 1
way1 = diag(X' * A * X);
% way 2
way2 = dot(X, A*X)';
% compare
[way1 way2]
How about this?
sum((A*X).*X,1)
Or, if you are dealing with complex values,
sum((A*X).*conj(X),1)
Check:
>> A = rand(4,4);
>> X = rand(4,3);
>> sum((A*X).*X,1)
ans =
5.4755 2.6205 3.4803
>> diag(X'*A*X)
ans =
5.4755
2.6205
3.4803
This could be one approach, though not sure if this would be more efficient than the direct matrix multiplication + diag based approach -
%// Perform X'*A equivalent multiplication
mult1 = bsxfun(#times,permute(X,[1 3 2]),A)
%// Perform rest of the equivalent multiplication
mult2 = bsxfun(#times,mult1,permute(X,[3 1 2]))
%// Perform the summations required to reduce to desired output's size
out = sum(reshape(mult2,[],size(X,2)),1)
You can re-arrange the multiplications a bit -
mult1 = bsxfun(#times,permute(X,[1 3 2]),permute(X,[3 1 2]))
mult2 = bsxfun(#times,mult1,A)
out = sum(reshape(mult2,[],size(X,2)),1)
Or merge the ending bsxfun(#times and sum with a bit more simplified and maybe more efficient version -
mult1 = bsxfun(#times,permute(X,[1 3 2]),permute(X,[3 1 2]))
out = reshape(permute(mult1,[3 1 2]),size(X,2),[])*A(:)
Or simplify it further to make it a one-liner that uses minimal of tools and could be the most efficient of the lot! -
out = reshape(bsxfun(#times,X.',permute(X,[2 3 1])),[],numel(A))*A(:)
In numpy, you can do np.einsum('ij,jk,ki->i',np.transpose(X),A,X).
I'm trying build a matlab function that will evaluate a function and vector that are sent in as parameters. I'm having a hard time trying to figure out how to send in the function so that it can be evaluated in the matlab function. I figured out how to do it without the function but I'm a little lost trying to evaluate it within a matlab function. Below is my code...
This is what I'm trying to do...
x = [x1 x2]';
f = x(x1)^2 + 2 * (x2)^2
x = [5 10];
f = (5)^2 + 2 * (10)^2 % which I would like to return 225, not a column vector
This is what I have and what I have tried...
x = [5 10]';
% without using a matlab function
% k = 1
% f = x(k)^2 + 2 * x(k + 1)^2; % returns the correct answer of 225
f = x^2 + 2 * x^2 % complains about the scalar 2
f = x.^2 + 2 * x.^2 % returns a column vector [75; 300]
function [value] = evalFunction(f,x)
value = f(x);
I've tried...
f = #(x) x.^2 + 2 * (x+1).^2;
value = evalFunction(#f,x) %Error: "f" was previously used as a variable
So I tried...
f = #(x) x.^2 + 2 * (x+1).^2;
value = evalFunction(f,x) %value = [97;342]
I'm new to matlab so any help is appreciated. I've been doing some research and found some stuff here on stackoverflow but can't seem to get it to work. I've seen there are other ways to do this, but I will eventually be adding more code to the matlab evalFunction function so I'd like to do it this way. Thanks!
Anonymous functions and function handles plus array indexing. Taking x as a 2-element vector, define and use your function like:
f = #(x) x(1).^2 + 2 * x(2).^2;
value = evalFunction(f,x) % but you can just do f(x) if that is all you need
However, if evalFunction does nothing other than evaluate f at x, then you don't need it at all. Just do f(x).
Alternately,
f = #(x1,x2) x1.^2 + 2*x2.^2;
value = evalFunction(f,x1,x2); % here your function will call it by f(x1,x2)
You are probably coming at this from a C background - in Matlab, x+1 is the entire vector x with 1 added - not the element offset by 1.
The function you need is
f = #(x)x(1).^2 + 2 * (x(2)).^2;
or, to be a little more "matlab-like":
f = #(x) [1 2] * x(1:2)'.^2;
Which performs the element-wise square of the first two elements of x as a column vector, and then does the matrix multiplication with [1 2], resulting in
1 * x(1) .^2 + 2 * x(2) .^2;
Which seems to be what you were asking for.
caveat: did not have opportunity to test this...
Given some multidimensional matrix A in Octave / Matlab,
What's the easiest way to get a matrix of the same size as A where all elements are replaced by their index along the k'th dimension
ie for the matrix
A =
ans(:,:,1) =
0.095287 0.191905
0.226278 0.749100
ans(:,:,2) =
0.076826 0.131639
0.862747 0.699016
I want a function f such that
f(A,1) =
ans(:,:,1) =
1 1
2 2
ans(:,:,2) =
1 1
2 2
f(A,2) =
ans(:,:,1) =
1 2
1 2
ans(:,:,2) =
1 2
1 2
and
f(A, 3) =
ans(:,:,1) =
1 1
1 1
ans(:,:,2) =
2 2
2 2
Also, given a sparse matrix B
What's the easiest way to get another sparse matrix of the same size where the nonzero elements are replaced by their index along the k'th dimension? (so same problem as above, but for only the nonzero elements)
Ideally I'm looking for a way which is well-vectorized for octave (meaning it doesn't explicitly loop over anything)
CLARIFICATION: For the sparse matrix one, I'm looking for a solution which does not involve creating a full size(B) matrix at any point
ndgrid() does what you want, although not in the format you are looking for. If you know the dims of the input A beforehand, you can use the following line to create the N-dimentional mesh grid:
% for matrix a where ndims(a) == 3
[x, y, z] = ndgrid (1:size(a,1), 1:size(a,2), 1:size(a,3));
% x is like f(a, 1)
% y is like f(a, 2)
% z is like f(a, 3)
You may be able to write a custom wrapper around ndgrid() to convert it to the function format you are looking for.
In case anyone's curious, since I didn't know about ndgrid, here's the answer I came up with:
function [y] = indices(a,k)
s = size(a);
n = s(k);
D = length(s);
x = permute(a,[k,1:(k-1),(k+1):D]);
y = reshape(x,n,[]);
y = diag(1:n) * ones(size(y));
y = reshape(y,size(x));
y = permute(y,[(2:k),1,(k+1):D]);
endfunction
function [y] = spindices(a,k)
s = size(a);
n = s(k);
D = length(s);
x = permute(a,[k,1:(k-1),(k+1):D]);
y = reshape(x,n,[]);
y = spdiag(1:n) * spones(y);
y = reshape(y,size(x));
y = permute(y,[(2:k),1,(k+1):D]);
endfunction
How can I partition a matrix into several smaller matrices to find unknown variables?
For example, given:
how can solve this problem by partitioning (splitting) a matrix 3x3 into smaller matrices (1x1 or other) to find the values of x, y, z and u?
Your matrix dimensions dont agree, or am I missing something?
Edit:
The code from Jeff E will work fine on smaller matrices.
For bigger matrices you will need to use backward substitution or some other algorithm, mainly because matrix inversion is a memory intensive task.
In the new image, you isolate for the unknown matrix using some identities:
A * X = B
(inv(A)) * A * X = (inv(A)) * B
I * X = (inv(A)) * B
X = (inv(A)) * B
In Matlab:
A = [1, 2; 0, 1]
B = [4, 7; 4, 6]
X = inv(A) * B
Output:
ans =
-4 -5
4 6
To solve an equation of the form A*X=B, you should use the backslash operator, since explicitly taking the inverse should be avoided if possible
A = [1, 2; 0, 1];
B = [4, 7; 4, 6];
X = A\B
X =
-4 -5
4 6