I have a tensor (L, L, H) ppa (where Area is L* L, and the height H is the number of time steps in a timeseries) and a vector naut (size H). I need to do the covariance between the timeseries in the tensor, so the all the columns, whose elements are: the 1st element is the element (i,j) in the 1st area, the 2nd is the element (i,j) in the second area and the to the H-th area, forming the tensor.
So, I reshape the tensor to have L*L columns of H elements to covariate with my vector.
ppa = reshape(paut, Area, H)';
Naut=naut';
my_cov = zeros(Area,1);
from the covariance matrix that I get from doing the covariance of every column with Naut, I want to take just the element (1,2) because it is symmetric and I only need the covariance.
for i=1:H
%cov matrix between the i-column and NAO
cov_tmp = cov(ppa(:,i), Naut);
%extracting the covariance element that I need from the covariance
%matrix
my_cov(i) = cov_tmp(1,2);
end
then I overwrite the vector my_cov to get a vector in which I have a series of covariance.
% if you want back a 120x120 matrix, just reshape
my_cov = reshape(my_cov,L,L);
the problem is that the vector my_cov keeps containing all zeros so the overwriting doesn't work.
Related
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));
Hi have a matrix A containing 132 elements of 3 features each
A= rand(132,3);
I compute the distance between each element
B=pdist(A);
and I put it in a squared distance matrix
C = squareform(B);
I compute the eigenvectors of the matrix
[V,D] = eig(C);
Now I would like to map a new element in the same space of eigenvectors V.
% I take the first element of A, but I assume it is a new one
new_element = A(1,:);
I calculate the distance with all the other elements of A
D1=pdist2(A(1,:),A);
now I can add this row to the C matrix obtaining
C2 = squareform([D1,B])
What I would like to do now is: given the eigenvector space V previously calculated. can I approximate the new row of C (i.e the first row of C2) in the same space (i.e. using the eigenvectors of C)?
Not sure what I say makes sense...I am just trying to make a new classifier, but I miss the last passage because I cannot recalculate the eigenvectors (calculated using the training set)
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.
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.
I would like to have a program that makes the following actions:
Read several matrices having the same size (1126x1440 double)
Select the most occuring value in each cell (same i,j of the matrices)
write this value in an output matrix having the same size 1126x1440 in the corresponding i,j position, so that this output matrix will have in each cell the most occurent value from the same position of all the input matrices.
Building on #angainor 's answer, I think there is a simpler method using the mode function.
nmatrices - number of matrices
n, m - dimensions of a single matrix
maxval - maximum value of an entry (99)
First organize data into a 3-D matrix with dimensions [n X m X nmatrices]. As an example, we can just generate the following random data in a 3-D form:
CC = round(rand(n, m, nmatrices)*maxval);
and then the computation of the most frequent values is one line:
B = mode(CC,3); %compute the mode along the 3rd dimension
Here is the code you need. I have introduced a number of constants:
nmatrices - number of matrices
n, m - dimensions of a single matrix
maxval - maximum value of an entry (99)
I first generate example matrices with rand. Matrices are changed to vectors and concatenated in the CC matrix. Hence, the dimensions of CC are [m*n, nmatrices]. Every row of CC holds individual (i,j) values for all matrices - those you want to analyze.
CC = [];
% concatenate all matrices into CC
for i=1:nmatrices
% generate some example matrices
% A = round(rand(m, n)*maxval);
A = eval(['neurone' num2str(i)]);
% flatten matrix to a vector, concatenate vectors
CC = [CC A(:)];
end
Now we do the real work. I have to transpose CC, because matlab works on column-based matrices, so I want to analyze individual columns of CC, not rows. Next, using histc I find the most frequently occuring values in every column of CC, i.e. in (i,j) entries of all matrices. histc counts the values that fall into given bins (in your case - 1:maxval) in every column of CC.
% CC is of dimension [nmatrices, m*n]
% transpose it for better histc and sort performance
CC = CC';
% count values from 1 to maxval in every column of CC
counts = histc(CC, 1:maxval);
counts have dimensions [maxval, m*n] - for every (i,j) of your original matrices you know the number of times a given value from 1:maxval is represented. The last thing to do now is to sort the counts and find out, which is the most frequently occuring one. I do not need the sorted counts, I need the permutation that will tell me, which entry from counts has the highest value. That is exactly what you want to find out.
% sort the counts. Last row of the permutation will tell us,
% which entry is most frequently found in columns of CC
[~,perm] = sort(counts);
% the result is a reshaped last row of the permutation
B = reshape(perm(end,:)', m, n);
B is what you want.