Matlab: multiplying rows of a matrix by vector elements - matlab

Let v be a row vector (1 x n matrix) and M be a n x m matrix.
I use the following piece of code to create a "weighted vector" (I hope the comments explain what it's supposed to be doing):
weighted_M = bsxfun(#times,v',M);
%creates a matrix with the i-th row of M being weighted (multiplied) by the i-th element of v
weighted_v = sum(weighted_M);
%sums the columns of weighted_M
Now the actual question: I have to do the same calculation for a lot of input vectors v. So instead I would like to input a matrix V that contains the vectors v as rows and output a matrix that contains the weighted vectors as rows. Is there any way to do this without using for loops?

If V is of size [k,n] and M is of size [n,m], and you're looking for the k weighted vectors, then you might simply need
weighted_vs = V*M;
an element of which is equal to
weighted_vs_ij = (V*M)ij = sum_l V_il * M_lj
First you multiply each row of M with a corresponding element of V (V_il * M_lj above for a fix i), then sum up as a function of the first index.
The result are the k weighted row vectors, each of length m.

Related

Indexing 3d matrix with 2d Matrix plus vector

I have a m * n * k Matrix called M which I want to index to get the mean of some Data.
I have a logical m * n matrix called EZG and want to apply this on every of the k-th dimension from 1:(end-1) (call this vector V).
Any chance to write it without a loop like this:
M=rand(3,3,3)
EZG=logical([1,1,1;0,1,0;0,0,1])
V=1:size(M,3)-1
mean(mean(M(EZG,V)1),2)
Result should be a onedimensional vector of the length of V.
Thank you
I think this is what you want:
M=rand(3,3,3);
EZG=logical([1,1,1;0,1,0;0,0,1]);
% repeat EZG K-1 times, and add zeros to the Kth slice
V=cat(3,repmat(EZG,1,1,size(M,3)-1),false(size(M,1),size(M,2)));
% logical index and mean
m=mean(M(V));

Mean of rows and columns of matrices in a cell

I have say n a x b matrices and I want to generate a new matrix of dimension a x b which is the mean of all n a x b matrices, i.e the first element of this new matrix is the mean of all first elements in each n a x b matrices and so on. Is there a way to compute this average matrix from a group of matrices in MATLAB? I had tried to do this by creating a cell but couldn't figure out how to take mean of each element of these matrices. I would appreciate any ideas or suggestions.
First, put your n matrix in a single axbxn matrix
M = cat(3, mat1, mat2, mat3, ...);
Or, if you work with a cell array,
M = cat(3, cellOfMats{:})
Then just use mean along the third dimension
meanmat = mean(M,3)

How do i find a matrix of 150*25 from two vectors such that each vector elements multiply with each element of another vector of dim 1*150 &1*25?

I have a vector created from linspace between specific numbers and have dimensions of 1*150. Now i want to multiply each element of the above created vector with another vector whose dimension is 1*25. The detail of my code is given below
c_p = linspace(1,.3*pi,150);
c = c_p';
C = zeros([150,25]);
for i= 1:1:size(C,1)
wp= c(i);
for n= 1:25
c_wp(n) = cos(n*wp);
end
C(i,25)= c_wp;
end
The vector is actually a multiple of cosine of length 25 and here wp is the elements of first vector of dimension 1*150. SO by the logic, I must have an output of 150*25 but instead giving me "subscripted assignment dimension mismatch". Any help would be appreciated, as i am new to matlab.
To multiply each element of a row vector a with each element of another row vector b, we can use linear algebra. We transpose a to make it a column vector and then use matrix multiplication:
a.' * b
That way you don't even need a for loop.

Multiply each column of a matrix by another matrix

I have a M x N matrix. I want to multiply each of the N columns by a M x M matrix. The following does this in a loop, but I have no idea how to vectorize it.
u=repmat(sin(2*pi*f*t),[n 1]);
W = rand(n);
answer = size(u);
for i=1:size(u,2)
answer(:,i) = W*u(:,i);
end
You simply need to multiply the two matrices:
answer = W*u;
Think about it: in every iteration of your loop you multiply a matrix by a vector. The result of that operation is a vector, which you save into your answer in column i. Matrix multiplication is a similar thing: you can understand it as multiplication of a matrix (W) by a set of vectors, which form your matrix u.
So your code is good, just remove the loop :)

2d matrix histogram in matlab that interprets each column as a separate element

I have a 128 x 100 matrix in matlab, where each column should be treated as a separate element. Lets call this matrix M.
I have another 128 x 2000 matrix(called V) composed of columns from matrix M.
How would I make a histogram that maps the frequency of each column being used in the second matrix?
hist(double(V),double(M)) gives the error:
Error using histc
Edge vector must be monotonically
non-decreasing.
what should I be doing?
Here is an example. We start with data that resembles what you described
%# a matrix of 100 columns
M = rand(128,100);
sz = size(M);
%# a matrix composed of randomly selected columns of M (with replacement)
V = M(:,randi([1 sz(2)],[1 2000]));
Then:
%# map the columns to indices starting at 1
[~,~,idx] = unique([M,V]', 'rows', 'stable');
idx = idx(sz(2)+1:end);
%# count how many times each column occurs
count = histc(idx, 1:sz(2));
%# plot histogram
bar(1:sz(2), count, 'histc')
xlabel('column index'), ylabel('frequency')
set(gca, 'XLim',[1 sz(2)])
[Lia,Locb] = ismember(A,B,'rows') also returns a vector, Locb,
containing the highest index in B for each row in A that is also a row
in B. The output vector, Locb, contains 0 wherever A is not a row of
B.
ismember with the rows argument can identify which row of one matrix the rows of another matrix come from. Since it works on rows, and you are looking for columns, just transpose both matrices.
[~,Locb]=ismember(V',M');
histc(Locb)