Finding solutions to a linear system in Matlab? - matlab

I have an equation like this
y = a*x+b;
I have sets of y and x
y = [1 2 3 4 5]
x = [6 7 8 9 10]
I want to find a and b, but not one solution; all solutions. I guess, I have to use polyfit, but I don't know how to do this and I don't understand why I have to use polyfit? Can you explain this to me?

From the polyfit documentation:
p = polyfit(x,y,n) finds the coefficients of a polynomial p(x) of
degree n that fits the data, p(x(i)) to y(i), in a least squares
sense. The result p is a row vector of length n+1 containing the
polynomial coefficients in descending powers:
So, you have data y at x-coordinates x, and you want to fit a first-degree polynomial to it. So use
p=polyfit(x,y,1);
and then p(1)=a and p(2)=b, or y=p(1)*x+p(2).
There are other ways to do this, but polyfit is very simple.

Related

What is the simplest way of finding the distance between two pixels?

This is maybe a bit of a noobish question - but say I want to find the distance between two pixels with coordinates (x1,y1) and (x2,y2). What would be the simplest way of doing this with MatLab?
pdist is an OK answer, but I would argue that it's slow (at least for a larger amount of points). Also, pdist requires the statistics toolbox, so if you don't have that toolbox, you can't use that answer.
I would suggest using bsxfun combined with permute and reshape instead for a toolbox independent solution. Assume that X is a 2 column matrix that is arranged in the following way:
X = [x y];
x and y are the X and Y coordinates of all of your points you want to find the distances to. Therefore, each row consists of a single query point:
X2 = permute(X, [3 2 1]);
out = sqrt(sum(bsxfun(#minus, X, X2).^2, 2));
out = reshape(out, size(X,1), []);
This should give you the same output as applying squareform to the output of pdist. Specifically, at element (i,j) of out, this will give you the distance between point i and point j and so the diagonal elements should give values of 0 as self-distances are 0.
Suggestion by Jonas
We can avoid reshape which may be costly by replacing it with another permute call if we slightly change the way we permute the dimensions before calculating the distances:
out = sqrt(sum(bsxfun(#minus, permute(X, [1 3 2]), permute(X, [3 1 2])).^2, 3));
X = [x1,y1;x2,y2];
d = pdist(X,'euclidean')
d is distance.

singular value decomposition and low rank tensorial approximation

according this article
http://www.wseas.us/e-library/conferences/2012/Vouliagmeni/MMAS/MMAS-07.pdf
matrix can be approximated by one rank matrices using tensorial approximation,i know that in matlab kronecker product plays same role as tensorial product,function is kron,now let us suppose that we have following matrix
a=[2 1 3;4 3 5]
a =
2 1 3
4 3 5
SVD of this matrix is
[U E V]=svd(a)
U =
-0.4641 -0.8858
-0.8858 0.4641
E =
7.9764 0 0
0 0.6142 0
V =
-0.5606 0.1382 -0.8165
-0.3913 0.8247 0.4082
-0.7298 -0.5484 0.4082
please help me to implement algorithm with using tensorial approximation reconstructs original matrix in matlab languages,how can i apply tensorial product?like this
X=kron(U(:,1),V(:,1));
or?thanks in advance
I'm not quite sure about the Tensorial interpretation but the closest rank-1 approximation to the matrix is essentially the outer-product of the two dominant singular vectors amplified by the singular value.
In simple words, if [U E V] = svd(X), then the closest rank-1 approximation to X is the outer-product of the first singular vectors multiplied by the first singular value.
In MATLAB, you could do this as:
U(:,1)*E(1,1)*V(:,1)'
Which yields:
ans =
2.0752 1.4487 2.7017
3.9606 2.7649 5.1563
Also, mathematically speaking, the kronecker product of a row vector and a column vector is essentially their outer product. So, you could do the same thing using Kronecker products as:
(kron(U(:,1)',V(:,1))*E(1,1))'
Which yields the same answer.

How to calculate the norm of quternion in Matlab?

How can I calculate the norm of quaternion in matlab?
I tried this example
a = [1 4 4 -4];
norm = quatnorm(a)
My expected output is 7 but matlab returns 49.
What am I doing wrong?
As #Dan points out, using the native implementation, you are probably getting the square of the formal norm definition. For some reason quatnorm returns the square, after estimating the Euclidean norm (square root of sum of squares).
q = [1 4 4 -4];
MATLABquatnorm:
for index = size(q, 1):-1:1
qnorm(index,:) = norm(q(index,:), 2);
end
qout = qnorm.*qnorm;
Alternative (for vectors):
sqrt(q*q')
This is equivalent to getting sqrt(quatnorm(q)). As you will note above, quatnorm is also adapted to estimate norms for quaternions stored in successive matrix rows (estimates the norm of each row and then squares)
Alternative (for matrices N x 4):
Q = [q; 2*q]; % example
sqrt(diag(Q*Q'))
You can either take square root of the returned number, or you can use function quatmod(q) instead, which calculates the proper Euclidean norm (modulus) of the complex number by not taking square of it.

MATLAB: Matrix containing values of another matrix at specific indices

I need help solving an indexing problem. The assigned problem states: Two matrices (x and y) give the coordinates to form matrix B from matrix A. Produce the matrix B which contains the values of A at the given coordinates in x and y.
For instance:
x = [1 1 1; 2 2 1]
y = [1 2 1; 3 2 4]
%This would read as (1,1),(1,2),(1,1),(2,3),(2,2),(1,4)
% Given matrix:
A = [6 7 8 9; 10 11 12 13];
%This would give us this answer for B (using the coordinate scheme above):
B=[6 7 6; 12 11 9];
I'm guessing I need to use the find function in conjunction with a sub2ind function, but I'm not 100% sure how to translate that into working code. The only thing I can think of would be to do something like this:
B=((x(1),(y(1)), (x(2),y(2)).......
But that would only work for the defined matrix above, not a randomly generated matrix. I tried looking for a similar problem on the site, but I couldn't find one. Your help would be really appreciated!
You can't do it for randomly generated matrices, because you have to ensure that matrix A has lines and columns as required from the values of x and y.
In this case, you can write:
for i=1:length(x(:))
B(i)=A(x(i),y(i));
end
B=reshape(B,size(x));

Given f(x) a polynomial in DocPolynom format, how to find f(x-1) as a polynomial? (MatLab)

I'm using DocPolynom a lot at the moment (see here if unfamiliar: http://www.mathworks.co.uk/help/techdoc/matlab_oop/f3-28024.html)
I have a polynomial f = DocPolynom(v) where v is a vector of coefficients. I really would like to be able to convert f to the polynomial corresponding to f(x-a), where a is a pre-determined constant. Does anyone know if/how I can do this?
Thanks!
Although not a direct answer, since you know the coefficients of the polynomial, you can evaluate the polynomial by polyval at the inputs x-a and using the resultant output you can use polyfit to get the coefficients of the polynomial that passes through your data.
v=[1 2 3];
x=1:3;
a=2;
y=polyval(v,x-a);
polyfit(x,y,2) % 2 here is the order of your polynomial (i.e. length(v)-1)
ans =
1.0000 -2.0000 3.0000
To do this, you need at least N+1 data points, where N is the order of your polynomial.
I'm not sure what this object you are writing is supposed to do, but you might play with my sympoly toolbox, which allows symbolic computation on polynomials. It is on the file exchange.
If all you have are simple polynomials, you can always use conv to compute powers of (x - a), adding them together. Thus, if we have the polynomial
P(x) = 3*x^2 + 2*x + 1
and we wish to form the polynomial Q(x) = P(x-3), it takes only a few operations.
Q = 3*conv([1 -3],[1 -3]) + 2*conv([0 1],[1 -3]) + 1*conv([0 1],[0 1])
Q =
3 -16 22