Separating some part of a matrix in matlab - matlab

I have a 2m by 2 matrix called A and a 2 by m matrix called B. Let's name the product of A by B, C:
C= A*B;
which C is a 2m by m matrix. I want to find matrix F which contains some parts of C. F is a m by 2 matrix and contains elements C(1,1), C(2,1), C(3,2),C(4,2),C(5,3), C(6,3),...C(2m-1,m), C(2m,m).
For example, consider
A = [0,2;1,3;4,7;8,3;4,5;1,2]
B=[1,4,6;5,7,3]
C=A*B;
In this case:
F=[C(1,1),C(2,1);C(3,2),C(4,2);C(5,3),C(6,3)]
But I like to find F without calculating all elements of C. Because I think calculating all elements of C would be time wasting for large values of m. Could anyone suggest a way to find F in general case?

Using Indexing:
F = [C(1:2*m+2:end);C(2:2*m+2:end)]'
To find F without calculating C you can instead use:
F=cell2mat(arrayfun(#(x) A(2*x-1:2*x,:)*B(:,x), 1:m,'uniformoutput',0))'
(You must set m, A, and B as defined in the question)
Explanation:
Each row of F is the transpose of the product of a submatrix in A and a column of B. For example, the first row in F is the transpose of:
A(1:2,:)*B(:,1)
The next row is the transpose of:
A(3:4,:)*B(:,2)
etc.
So this method only calculates the necessary values, by multiplying each column of B only by the corresponding submatrix of A and avoids calculating the unused values in C.

Related

MATLAB function that gives all the positive integers in a column vector

I need to create a function that has the input argument n, a integer , n>1 , and an output argument v, which is a column vector of length n containing all the positive integers smaller than or equal to n, arranged in such a way that no element of the vector equals its own index.
I know how to define the function
This is what I tried so far but it doesn't work
function[v]=int_col(n)
[1,n] = size(n);
k=1:n;
v=n(1:n);
v=k'
end
Let's take a look at what you have:
[1,n] = size(n);
This line doesn't make a lot of sense: n is an integer, which means that size(n) will give you [1,1], you don't need that. (Also an expression like [1,n] can't be on the left hand side of an assignment.) Drop that line. It's useless.
k=1:n;
That line is pretty good, k is now a row vector of size n containing the integers from 1 to n.
v=n(1:n);
Doesn't make sense. n isn't a vector (or you can say it's a 1x1 vector) either way, indexing into it (that's what the parentheses do) doesn't make sense. Drop that line too.
v=k'
That's also a nice line. It makes a column vector v out of your row vector k. The only thing that this doesn't satisfy is the "arranged in such a way that no element of the vector equals its own index" part, since right now every element equals its own index. So now you need to find a way to either shift those elements or shuffle them around in some way that satisfies this condition and you'd be done.
Let's give a working solution. You should really look into it and see how this thing works. It's important to solve the problem in smaller steps and to know what the code is doing.
function [v] = int_col(n)
if n <= 1
error('argument must be >1')
end
v = 1:n; % generate a row-vector of 1 to n
v = v'; % make it a column vector
v = circshift(v,1); % shift all elements by 1
end
This is the result:
>> int_col(5)
ans =
5
1
2
3
4
Instead of using circshift you can do the following as well:
v = [v(end);v(1:end-1)];

Right Array Division : Ignoring division by zeroes

I have two data matrices A and B of similar dimensions. I intend to divide each element of A by its corresponding elements of B. For this matlab provides a shortcut as C = A./B. However, B has many zero elements , for such elements I want elements of C to be zero rather than NAN . Does MATLAB provide a way to do this in an efficent manner? I could do this in a loop but that would be very expensive.
Thanks.
Yes. You can use logical indexing:
C = zeros(size(A));
t = logical(B);
C(t) = A(t)./B(t);
With logical indexing, only the elements of A, B and C corresponding to true elements of t will be evaluated. t is true only where B is non-zero. Note that C is pre-initialized to zeros to automatically take care of the cases that are not evaluated because B is zero.

Maximum of a subset of array (MATLAB)

Suppose in MATLAB I have a real matrix A which is n x m and a binary matrix B of the same size. The latter matrix defines the optimization set (all indices for which the element of B equals one): over this set I would like to find the maximal element of A. How can I do this?
The first idea I had is that I consider C = A.*B and look for the maximal element of C. This works fine for all matrices A which have at least one positive element, however it does not work for matrices with all negative elements.
You can do
C = A(B==1);
to give you an array of just the values of A corresponding to a value of 1 in B. And
max( C )
will give you the maximum value of A where B is 1
With this method you don't run into a problem when all values of A are negative as the zeros don't appear in C.
Obviously you can condense this to
desiredValue = max(A(B(:)==1));
I am using the colon operator to make sure that the result of A(B(:)==1) is a column vector - if B is all ones I am not sure if Matlab would return a vector or a nxm matrix (and I can't confirm right now).
update to get the index of the value, you can do:
f = find(B==1);
[m mi] = max(A(f));
maxIndex = f(mi);
And to get that back to the 2D elements:
[i j] = ind2sub(size(A), maxIndex);

Efficient way of concatenating vectors

I have a n x 3 matrix E, a lot of means stocked in a d x 3 matrix M and a covariance matrix, say identity.
I want to compute, for each point in M, the mvnpdf(E[i,:],M(k,:),cov).
Basically, when I run mvnpdf(E,M(k,:),cov), I get a vector
[mvnpdf(E(1,:),M(k,:),cov)
mvnpdf(E(2,:),M(k,:),cov) etc]
I want to cat these vectors to get a matrix like:
[mvnpdf(E,M(1,:),cov), mvnpdf(E,M(2,:),cov), etc]
Is there any way to do that without a for loop?
This works on my machine, but see if it is what you are after:
Cov = eye(3);
C = arrayfun(#(x,y,z) mvnpdf(E,[x y z],Cov), M(1,:), M(2,:), M(3,:),'uni',false);
A = [C{:}]
Note: Consider not using cov as a variable because it is a MATLAB function.
EDIT: My original output M clobbered your input M. Please try again with original data!

Multiplying a 3x3 matrix to 3nx1 array without using loops

In my code, I have to multiply a matrix A (dimensions 3x3) to a vector b1 (dimensions 3x1), resulting in C. So C = A*b1. Now, I need to repeat this process n times keeping A fixed and updating b to a different (3x1) vector each time. This can be done using loops but I want to avoid it to save computational cost. Instead I want to do it as matrix and vector product. Any ideas?
You need to build a matrix of b vectors, eg for n equal to 4:
bMat = [b1 b2 b3 b4];
Then:
C = A * bMat;
provides the solution of size 3x4 in this case. If you want the solution in the form of a vector of length 3n by 1, then do:
C = C(:);
Can we construct bMat for arbitrary n without a loop? That depends on what the form of all your b vectors is. If you let me know in a comment, I can update the answer.