Find entrance of matrix for each adjacent pair of numbers in vector and multiply - matlab

I have a (transition) function defined by a matrix say P=[0.75,0.25;0.25,0.75] and I have a vector say X=[1,2,1] then i would like to find P(1,2)*P(2,1). How is the easiest way to generalise this? I tried creating a function handle for P(i,j) and then X_temp=[X(1:end-1);X(2:end)], using the function of each column and finally using the product function, but it seems a lot more comprehensive than it has to be.
The X i want to use is 1000 dimensional and P is 3x3 and I would have to repeat it a lot of times so speed I think will matter.

You can use sub2ind to get your relevant P values:
Ps = P(sub2ind(size(P), X(1:end-1), X(2:end)))
Now just multiply them all together:
prod(Ps)
EDIT:
For function handles you had the right idea, just make sure that you function itself handles vectors. For example lets say your function f(i,j) = i + j, I'm going to assume it's actually f(x) = x(1) + x(2) but I want it to handle many xs at once sof(x) = x(:,1) + x(:,2):
f = #(x)(x(:,1) + x(:,2))
f([X(1:end-1)', X(2:end)'])
OR
f = #(ii, jj)(ii + jj)
f(X(1:end-1)', X(2:end)') %//You don't actually need the transposes here anymore
just note that you need to use element wise operators such as .*, ./ and .^ etc instead of *, /,^...

Related

Implementing a Function using for-loops and matrix multiplication in matlab

My goal is to implement a function which performs fourier synthesis in matlab, as part of learning the language. The function implements the following expression:
y = sum(ak*exp((i*2*pi*k*t)/T)
where k is the index, ak is a vector of fourier coefficients, t is a time vector of sampled times, and T is the period of the signal.
I have tried something like this:
for counter = -N:1:N
k = y+N+1;
y(k) = ak(k)*exp((i*2*pi*k*t)/T);
% y is a vector of length 2N+1
end
However, this gives me an error that the sides do not have equal numbers of items within them. This makes sense to me, since t is a vector of arbitrary length, and thus I am trying to make y(k) equal to numerous things rather than one thing. Instead, I suspect I need to try something like:
for counter = -N:1:N
k=y+N+1;
for t = 0:1/fs:1
%sum over t elements for exponential operation
end
%sum over k elements to generate y(k)
end
However, I'm supposedly able to implement this using purely matrix multiplication. How could I do this? I've tried to wrap my head around what Matlab is doing, but honestly, it's so far from the other languages I know that I don't really have any sense of what matlab's doing under the hood. Understanding how to change between operations on matrices and operations in for loops would be profoundly helpful.
You can use kron to reach your goal without for loops, i.e., matrix representation:
y = a*exp(1j*2*pi*kron(k.',t)/T);
where a,k and t are all assumed as row-vectors
Example
N = 3;
k = -N:N;
t = 1:0.5:5;
T = 15;
a = 1:2*N+1;
y = a*exp(1j*2*pi*kron(k.',t)/T);
such that
y =
Columns 1 through 6:
19.1335 + 9.4924i 10.4721 + 10.6861i 2.0447 + 8.9911i -4.0000 + 5.1962i -6.4721 + 0.7265i -5.4611 - 2.8856i
Columns 7 through 9:
-2.1893 - 4.5489i 1.5279 - 3.9757i 4.0000 - 1.7321i

Octave complex element-wise operations

Is there a way to write 'complex' element-wise operations in one line, or do we have to separate them into multiple lines?
For example, let us have this mathematical function: 1/(1+e^-x)
Which I want to calculate for each element on x (x may be a scalar, vector or a matrix).
This is a working code I have written:
function r = sigmoid(x)
r = zeros(size(x));
r = e.^(-x);
r = 1.+r;
r = 1./r;
end
My question is - can we simplify it to one line?
yeah you can do it by below function
function r=sigmoid(x)
r=1./(1+exp(-x))
end
this first consider that the exp function calculate exponential values wise element and added by one and finally result divided 1 over matrix element wise and you can get what you want.

how to define an array with fractional index number

Like suppose that I need to create a function named pressure denoted by p (a 2-D matrix) which depends on 2 variables r and z.
u, v, w are linear matrices which also depend on 2 variables r and z.
r and z are linear matrix defined below take i={1,2,3,4,5,6,7,8,9,10}
r(i)=i/10
z(i)=i/10
u(i) = 2*r(i) + 3*z(i)
v(i) = 8*r(i) + 4*z(i)
w(i) = 3*r(i) + 2*z(i)
p = p(r,z) %, which is given as,
p(r(i),z(j)) = 2*v(i) - 4*u(i) + w(j)
Now suppose the value of p at a given location (r,z) say (0.4,0.8) is needed, I want that if I give the input p(0.4,0.8), I get the result.
In your case the easiest way is to convert the fractional numbers to integers by multiplying by 10.
This way the location (r,z) = (0.4, 0.8) will become (4,8).
If you don't want to remember every time to provide the locations multiplied by 10, just create a function that will do it for you, so you can call the function with the fractional location.
If your matrices are linear, you will always find a multiplying factor to get rid of the fractional coordinates.
Not entirely sure what you mean here, but if your matrix is only defined in the indices you give (i.e. you only want to draw values from the fixed set of indices you defined), then this should do it:
% the query indices
r_i = 0.4;
z_i = 0.8;
value = p(r_i*10,z_i*10);
if you want to look at values between the ones you defined, you need to look at interpolation:
% the query indices
r_i = 0.46;
z_i = 0.84;
value = interp2(r,z, p, r_i, z_i);
(I may have gotten r and z in that last function in the wrong order, try it out).

Row--wise application of an inline function

I defined an inline function f that takes as argument a (1,3) vector
a = [3;0.5;1];
b = 3 ;
f = #(x) x*a+b ;
Suppose I have a matrix X of size (N,3). If I want to apply f to each row of X, I can simply write :
f(X)
I verified that f(X) is a (N,1) vector such that f(X)(i) = f(X(i,:)).
Now, if I a add a quadratic term :
f = #(x) x*A*x' + x*a + b ;
the command f(X) raises an error :
Error using +
Matrix dimensions must agree.
Error in #(x) x*A*x' + x*a + b
I guess Matlab is considering the whole matrix X as the input to f. So it does not create a vector with each row, i, being equal to f(X(i,:)). How can I do it ?
I found out that there exist a built-in function rowfun that could help me, but it seems to be available only in versions r2016 (I have version r2015a)
That is correct, and expected.
MATLAB tries to stay close to mathematical notation, and what you are doing (X*A*X' for A 3×3 and X N×3) is valid math, but not quite what you intend to do -- you'll end up with a N×N matrix, which you cannot add to the N×1 matrix x*a.
The workaround is simple, but ugly:
f_vect = #(x) sum( (x*A).*x, 2 ) + x*a + b;
Now, unless your N is enormous, and you have to do this billions of times every minute of every day, the performance of this is more than acceptable.
Iff however this really and truly is your program's bottleneck, than I'd suggest taking a look at MMX on the File Exchange. Together with permute(), this will allow you to use those fast BLAS/MKL operations to do this calculation, speeding it up a notch.
Note that bsxfun isn't going to work here, because that does not support mtimes() (matrix multiplication).
You can also upgrade to MATLAB R2016b, which will have built-in implicit dimension expansion, presumably also for mtimes() -- but better check, not sure about that one.

Simple MATLAB Variable Question

Please help me write a MATLAB program that constructs a column matrix b, such that
b1 = 3x1 - 3/4y0
b2 = 3x2
...
bn-2 = 3xn-2
bn-1 = 3xn-1 - 3/4yn
where x and y are variables. Notice that y only appears in the first and last entries of b.
My problem is that I don't know how variables work in MATLAB. I tried
b = 3*x
and it says
??? Undefined function or variable 'x'
So, how do we create variables instead of constants?
Thanks!
EDIT:
From your comments above, what you need is MATLAB's symbolic toolbox, which allows you to perform computations in terms of variables (without assigning an explicit value to them). Here's a small example:
syms x %#declare x to be a symbolic variable
y=1+x;
z=expand(y^2)
z=
x^2 + 2*x + 1
You will need to use expand sometimes to get the full form of the polynomial, because the default behaviour is to keep it in its simplest form, which is (1+x)^2. Here's another example to find the roots of a general quadratic
syms a b c x
y=a*x^2+b*x+c;
solve(y)
ans =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
I think you meant bn and xn in the last line... Anyway, here's how you do it:
b=3*x;
b([1,end])=b([1,end])-3/4*y([1,end])
You can also do it in a single line as
b=3*x-3/4*[y(1); zeros(n-2,1); y(end)];
where n is the length of your vector.
You never stated your problem...
Anyways just set the first entry of b individually first. Then use a loop to set the next values of b from 2 up to n-2. Then set the last entry of b individually.
On a side note, if x is a vector, you can simply vectorize the loop part.