What does this command do? - matlab

I am new at MATLAB, help me understand this
I googled a lot but I couldn't find a correct answer
Any links will also be helpful
[x, fs] = wavread('bee.wav'); This returns the audio files sampling rate in x
x = x(1000:1480);
What does this do?
I know x(:) makes a column vector, but x(a:b); does it make an m * n matrix an n * m?

The command x = x(1000:1480); takes a slice out of a given array and puts it back to x.
These are basics; you should have a look at a good tutorial.
Examples:
a = [1 2 3 4 5];
b = a(2:2:5) % -> [2 4]
c = a(3:end) % -> [3 4 5]

Related

Matlab How to iterate a function through a list

I need some help iterating over a function that depends on two variables.
So let's say I have a function that depends on two variables.
Let's call it f = x + y
Now, I have a two lists, one of x variables and one of y variables. For example,
xlist = [1 2 3] and
ylist = [4 5 6]
And I want to go through each element of the lists and plug it into f. For example, set x=1 and then evaluate f for y=4, then y=5, then y=6... and repeat for x=2 and x=3.
Finally, I want to return a matrix of the calculated values. For the example above, the answer should be
[5 6 7];[6 7 8];[7 8 9]
How would I go about doing this?
Assuming the function can't be vectorized and thus you really need to iterate, a simple way is via ndgrid (to create x, y values that describe all possible pairs) and arrayfun (to iterate over the two x, y values at the same time):
f = #(x,y) x+y; % Or f = #fun, if fun is a function defined elsewhere
xlist = [1 2 3];
ylist = [4 5 6];
[xx, yy] = ndgrid(xlist, ylist); % all pairs
result = arrayfun(f, xx, yy);
In many cases the function can be vectorized, which results in faster code. For the example function this would be done defining f as
f = #(x,y) bsxfun(#plus, x(:), y(:).');
or in recent Matlab versions you can exploit implicit singleton expansion and just write
f = #(x,y) x(:)+y(:).';
In either case, note that the two arguments of addition are forced to be a column vector (x(:)) and a row vector (y(:).'), so that singleton expansion will automatically create all pairs:
xlist = [1 2 3];
ylist = [4 5 6];
result = f(xlist, ylist);
Another way to do it would be to use two for loops:
x_values=[2,4,6,8];
y_values=[1,3,5,7];
%initialize a matrix with zeros
output_matrix=zeros(length(x_values),length(y_values));
for i=1:length(x_values)
for j=1:length(y_values)
output_matrix(i,j)=x_values(i)+y_values(j);
end
end
output_matrix

Multiply each column in a matrix by corresponding row in another and sum results in Matlab

Lets say I have matrices A = [1 2; 3 4], B = [4 3; 2 1]. I want to multiply each column from matrix A ([1; 3], [2; 4]) by the corresponding row in matrix B ([4 3], [2 1]) and sum resulting matrices. I have came up with the following code:
C = zeros(size(A));
for i = 1 : size(A, 1)
C = C + A(:, i) * B(i, :);
end
Could it be rewritten using some math trick or matlab function to get rid of the for loop?
I see there is unclarity in my question regarding the result I want. The result should exactly mimic provided Matlab code, therefore I seek one matrix which is given by the matrix summation of the intermediate matrices that are created by multiplying each column vector with corresponding row vector from both matrices. For this specific example, it would be given by
C = A(:, 1) * B(1, :) + A(:, 2) * B(2, :);
I am just looking for some generic, for-loop less version for any matrices of compatible dimensions.
I just tried out my suggestion in the comments, and it seems to work with this octave tester:
Short form (only works in Octave):
A = [1 2; 3 4], B = [4 3; 2 1]
X = sum((A * B)(:))
Long form (Matlab):
A = [1 2; 3 4]
B = [4 3; 2 1]
C = A * B % Stop here if you want the exact result from your Matlab code
x = sum(C(:)) % To get the sum of the resulting matrix
Sources:
https://www.tutorialspoint.com/matlab/matlab_matrix_multiplication.htm
https://www.mathworks.com/matlabcentral/newsreader/view_thread/51252
Update, based on your update:
Output of A * B:
8 5
20 13
Output of your code:
8 5
20 13
It appears that
C = zeros(size(A));
for i = 1 : size(A, 1)
C = C + A(:, i) * B(i, :);
end
is equivalent to the matrix multiplication
C = A*B
for square matrices A and B.
You can also do this in MATLAB, to get the sum.
C=ones(1,2)*A*B*ones(2,1)
The general form would be
C=ones(1,size(A,1))*(A*B)*ones(size(B,2),1);
EDIT
I see you updated your question for clarity. The matrix product can be calculated directly
C = A*B;
as pointed out by jodag.
This works provided you follow rules of linear algebra where inner dimensions of your matrices are the same (i.e. when number of columns in A match the number of rows in B; size(A,2)==size(B,1)).

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

using size of scatter points to weight line of best fit in matlab

Is it possible to use the size (s) of the points to 'weight' the line of best fit?
x = [1 2 3 4 5];
y = [2 4 5 3 4];
s = [10 15 20 2 5];
scatter(x,y,s)
hold on
weight = s;
p = polyfit(x,y,1); %how do I take into account the size of the points?
f = polyval(p,x);
plot(x,f,'-r')
Using Marcin's suggestion, you can incorporate lscov into polyfit. As the documentation explains, polynomial fitting is done by computing the Vandermonde matrix V of x, and then executing p = V\y. This is the standard formalism of any least-squares solution, and lends itself to weighted-least-squares in MATLAB through lscov.
Taking your x, y and weight vectors, instead of calling polyfit(x,y,n) you can do the following:
% Construct Vandermonde matrix. This code is taken from polyfit.m
V(:,n+1) = ones(length(x),1,class(x));
for j = n:-1:1
V(:,j) = x.*V(:,j+1);
end
% Solve using weighted-least-squares
p = lscov(V,y,weight);
You can even go one step further, and modify polyfit.m itself to include this functionality, or add another function polyfitw.m if you are not inclined to modify original MATLAB functions. Note however that polyfit has some more optional outputs for structure, computed using QR decomposition as detailed in the documentation. Generalization of these outputs to the weighted case will require some more work.
x = (1:10)';
y = (3 * x + 5) + rand(length(x),1)*5;
w = ones(1,length(y));
A = [x ones(length(x),1)];
p = lscov(A,y,w);
plot(x,y,'.');
hold on
plot(x,p(1)*x + p(2),'-r');

Extract rows from matrix and make a new matrix in MATLAB

I have a matrix
X = [1 1;2 2;3 3;4 4];
Y = [2 4];
I want a resulting matrix z to have just rows 2 and 4 (the values in Y) of X. That is,
Z = [2 2;4 4];
Any solutions?
Z = X(Y,:);
This is a pretty easily researched question in my opinion: the first result for "MATLAB matrix indexing" answers your question and has a lot more general information about selecting parts of MATLAB matrices.