Matlab, Getting rid of for loop using one liner - matlab

Lets assume we have some code. In Matlab editor:
x = zeros(1,10);
x(1,1) = 2;
for k = 1: 9
x(k+1) = 10 * x(k);
end
Is it possible to write the equation without the for loop?

Try this:
x = 2 * 10.^(0:9);
Hope that helps.

Check out the logspace function:
x=2*logspace(0,9,10)

You have an error because it is contrary to the rules of matrix multiplication.
My solution below, I used the free analogue of Matlab - Octave, which has a similar syntax:
X=randint(2) % Matrix of size 2 by 2
X =
1 0
0 0
Y=2 * 10.^X(:)
Y =
20
2
2
2
You have a right to multiply the matrix only this type:
M x N on N x P
The result is a matrix of the following dimensions:
M x P
See also:
Element-wise multiplication
Element-wise power
I hope this helped

Related

Octave Coding - I need help coding coefficients of polynomial

This question fairly easy doing it manually however, I am struggling to have this written in code.
There is a quartic polynomial:
P(x)=ax^4+bx^3+cx^2+dx+e
There is also a given matrix M:
5 0 -1 2 9
-2 -1 0 1 2
Which the first row gives P(x) and the second row gives the value of x.
Using the information in matrix M, find the coefficients:
a, b, c, d, e
I would know how to work this manually, subbing each column and solve simultaneously with the other columns to obtain a value for each coefficient or put it in a matrix.
I have an idea of what to do, but I don't know how to code it.
I do believe the last line would be linearsolve(M(,1),M(,2)) and thus be able to obtain each coefficient but I have no idea how to get to that line.
Welcome J Cheong
% Values of y and x (note: in your post you had 2 values at x = 1, I assumed that was an accident)
M = [5 0 -1 2 9 ; -2 -1 0 1 2];
% Separate for clarity
y = M(1,:);
x = M(2,:);
% Fit to highest order polynomial model
f = fit(x',y',['poly', num2str(length(y)-1)])
% Extract coefficients
coeff = coeffvalues(f);
% Plotting
X = linspace(min(x)-1, max(x) + 1, 1000) ;
plot(x,y,'.',X,f(X))
Edit
Sorry, I'm using Matlab. Looking at the Octave documentation. You should be able to get the coefficients using
p = polyfit(x,y,length(y)-1)';
Then to display the coefficients the way you specified try this
strcat(cellstr(char(96+(1:length(p))')), { ' = ' } , cellstr(num2str(p)))
y=[5 0 -1 2 9];
x=[-2 -1 0 1 2];
P=polyfit(x,y,2)
gives
P =
2.0000 1.0000 -1.0000
these are your coefficients for c,d,e the others are zero. You can check the result:
polyval(P, x)
ans =
5.0000e+00 2.2204e-16 -1.0000e+00 2.0000e+00 9.0000e+00
which gives you y
Btw, you can solve this very fast just inside your head without calculator because the function values for x=0 and x=+/-1 are very easy to calculate.

How to plot discrete signals (delta equation)?

Plot 2 discrete signals:
x[n] = delta[n] - delta[n-1] + delta[n+4]
y[n] = 0.5^n*u[n]
Also plot the convolution.
I don't know what the delta is supposed to be and how to approach these kind of signals. If I have a simple signal, I know how to do it.
n = 0:7;
x1 = cos(pi*n);
subplot(1,2,1)
stem(n,x1)
Using the dirac (delta) function in matlab will not work for discrete functions as the outcome is Inf at n=0. Instead use the value 1 at the right locations. Furthermore, u[n] is the step function or in matlab the heaviside function. It is zero for negative x and 1 for positive x, making a step at exactly x = 0.
The following code will plot all your functions:
n = -5:5
x = [0 1 0 0 0 1 1 0 0 0 0]; %x[n] from n =-5 to n=5
%y = 0.5.^n .* heaviside(n); %[y[n] from n =-5 to n=5
y = 0.5.^n .* [0 0 0 0 0 1 1 1 1 1 1]; %stepfunction from n =-5 to n=5
z = conv(x,y); %z[n] from n = -10 to n=10
subplot(3,1,1);stem(n,y1)
subplot(3,1,2);stem(n,y2)
subplot(3,1,3);stem(-10:10,y3)
It appears to the be the Dirac delta function. Which has a function in Matlab.
x = dirac(n)
Also, the convolution of two functions has a function.
w = conv(u,v)
Not knowing the interval you have for these, I can't say. I could generate some code. Also the function u is unknown.

Matlab/ GNU octave syntax

I need to understand a part of the GMRES algorithm written in these languages. What makes me a problem is the following piece:
y = H(1:k,1:k) \ beta(1:k);
x = x + Q(:,1:k)*y;
Can someone explain what does it mean in extended form.
Thank you in advance.
For what concerns the first equation:
H(1:k,1:k) = sub-matrix of matrix H that you obtain by taking rows from 1 (beginning) to k and columns from 1 (beginning) to k
beta(1:k) = sub-vector of vector beta that you obtain by taking elements from 1 (beginning) to k
y = is a matrix obtained by solving a symbolic matrix left division between sub-matrix of H and the sub-vector of beta
For what concerns the second equation:
Q(:,1:k) = sub-matrix of matrix Q with all the rows and columns from 1 (beginning) to k
x = a matrix that is obtained by adding to it's previous value the result of the multiplication between the sub-matrix of matrix Q and y
Indexing in Matlab is 1-based, not 0-based. So index 1 corresponds to the first element of whatever you are working with. Example of sub-matrix by indexing:
A = [
2 3 4;
1 2 3;
3 4 4
];
B = A(1:2,1:2);
B is then equal to:
[
2 3;
1 2
];
C = A(:,1:2);
C is then equal to:
[
2 3;
1 2;
3 4
];
That weird division symbol represents a matrix left division (for more information: mathworks.com/help/symbolic/mldivide.html): X = A\B solves the symbolic system of linear equations in matrix form: A*X = B for X.

Matlab: work with 2NxN matrix

I have a matrix 2NxN.
And I want get some parametrs by this matrix. For example it:
How, I can do it?
You may want to break your 12x6 matrix, into two 6x6 matrix; let's say: Z and Zb (last one for z bar). Odd rows are Z and evens are Zb.
Considering M to be the combined matrices:
Z = M(1:2:end,:)
Zb = M(2:2:end,:)
read about the colon(:) operator and end to see what 1:2:end means.
Hope it helps.
From what I understand here are the first three:
% Random Matrix
% Needs to be defined before the functions since the functions look for
% the m variable
m = rand(12,6);
% Function 1
p = #(i,j) sign(m(i,j)+m(i+1,j)) * max(abs(m(i,j)),abs(m(i+1,j)));
p(2,2)
% Function 2 - Avg of row
pavg = #(i) mean(m(i,:));
pavg(2)
% Function 3
c = #(i,j) abs(m(i,j)+m(i+1,j)) / (abs(m(i,j)) + abs(m(i+1,j)));
c(2,2)

How to do elimination in matlab?

How to calculate this kind of matrix :
A = [ 1 3 4
4 5 7
10 8 6]
X= [x1
x2
x3]
Y= A*X=0
we can change it into :
1x1+3x2+4x3=0
4x1+5x2+7x3=0
10x1+8x2+6x3=0
How to do elimination in Matlab to get the x1, x2 and x3??
I'm not 100% sure what you're asking.
I suppose you want a way to solve a SLE. There are few way to this, the one that I personally find more straightforward is
x=A\b
where in your case:
b=zeros(3,1)
Note that you don't need the vector you're calling X as MATLAB will automatically consider the values in A as coefficient of different variables
My guess is you created an example for this question, but didn't really check if it would produce the desired result. For Ax = 0 to have a non-zero solution, the determinant det(A) must be zero. As the determinant of your A matrix is 40, the only solution is x = [0; 0; 0].
Now, assuming you picked a better example, such as:
A = [1 2 3;2 4 6;1 1 1];
Now, det(A) = 0.
Using regular linsolve, you will still only get x = [0; 0; 0] as a solution. However, there are clearly (infinitely) many other non-trivial solutions. You can achieve one such solution using null like this:
x = null(A)
x =
0.4082
-0.8165
0.4082
A*x
ans =
1.0e-014 *
0.1110
0.2220
0.0944
Which is practically zero (inaccuracies are due to floating point precision).
You can also use Singular value decomposition, svd to get the same results:
[U S V] = svd(A);
x = V(:,end)
x =
0.4082
-0.8165
0.4082
A*x
ans =
1.0e-014 *
0.1110
0.2220
0.0944