Correlation and array in MATLAB - matlab

Kindly somebody help me in this.
I have two arrays of equal size 8x8.
And I need covariance of Column 1 of array 1 with the column 1 of second one.
After that I want to find Column 1 with the column 2 of second array.
After that I want to find Column 1 with the column 3 of second array.
After that I want to find Column 1 with the column 4 of second array.
After that I want to find Column 2 with the column 1 of second array.
And so on

I am assuming you want a measure as to how a column of first array varies with a column of second array. If yes, then that will be a scalar, otherwise, if you calculate the covariance matrix of two vectors, it is going to be a... matrix, obviously.
The following solution is based on the fact that covariance of two vectors is their correlation times the product of their standard deviations. More succinctly, for two random variables X and Y,
cov(X,Y)=corr(X,Y)*(sd(X)*sd*Y))
Thus, the solution to your question is:
pairCovariance=corr(X,Y).*(std(X).'*std(Y))

Related

given the 10x10 matrix m:(10 10)#100?1f In kdb/q

given the 10x10 matrix m:(10 10)#100?1
Find the average of every row and column of the matrix
Find the average of the matrix
Take the first element from the first row, the second from the second row etc
find the diagonal elements of a matrix
First I'm going to change the input so you can better see the solution. The 100?1 will give a list of 100 0s which I don't think is what you want. Maybe you wanted either 100 random (0;1) or 100 random values between 0-1. I went for the latter.
q)show m:(10 10)#100?1.
0.4655548 0.8455166 0.7281041 0.7403385 0.5199511 0.199172 0.9548708 0.498..
0.86544 0.3112134 0.3520122 0.4485896 0.6742543 0.2357538 0.7589261 0.318..
0.7053699 0.8153197 0.5051956 0.7546554 0.08613905 0.7824787 0.2080171 0.282..
So, now the questions.
Find the average of every row and column of the matrix.
q)meanRows:avg each m
q)meanCols:avg each flip m
UPDATE From Comment. You can get the average of a matrix column without using each or flip but will return null if any element is null. Another note is that if a column is length 10, 5 of which are null. Then the avg will only consider the average of the 5 non-null values.
So, if you believe there are nulls you may want to get rid of them and then get the mean values:
q)m:^[0;m] //Replace null with 0s if necessary
q)meanCols:avg m //Get avg without flipping or using each
Find the average of the matrix
q)avg avg each m
I think that^ is the quickest way to get the overall mean because it doesn't require razing or flipping.
Take the first element from the first row, the second from the second row etc
q)getVector:{[mtx]mtx'[c;c:til count mtx]}
q)getVector m
0.4655548 0.3112134 0.5051956 0.6333324 0.7258795 0.8671843 0.7556175 0.17954..
Let me know if you have any further questions.

MATLAB indexing for a matrix

If T is a 3x3 matrix.. what does T(1:2,3) = k mean?
I think T(1:2) means the first 2 columns. So does T(1:2,3) mean the first two columns in the last row..?
You are assigning the value k to the first two rows of column 3.

MATLAB matrix operation

I am having matrix with approx 3000 rows(changing) and 3 columns.
I have count of both rows and columns.
I am trying to plot the graph:
x=1:3000;
plot(matrix(x,1))
is there any way that I can include all rows in the plot instruction itself so that I can remove 'x=1:3000' ?
Also, I want to divide, 1st column of matrix which have 3000 rows into another matrix of 3 columns each with 1000 rows. Any specific instruction for this ?
I have made for loop for this and then i am placing individually the elements in the new array. But its taking long time.
As to the plotting issue, using the colon operator will plot all rows for your desired column:
plot(matrix(:,1));
EDIT: You mentioned you were a beginner. In case you haven't seen the colon operator used like this before, a colon operator all by itself when indexing into a matrix essentially means "all __", either "all rows" if in the first position or "all columns" if in the second position.
As for the second question, of splitting one column into a new matrix with multiple columns, you can use the reshape() function, which takes the input matrix to be reshaped and a number of output rows and columns. For example, to split the first column of matrix into 3 columns and put them into newMatrix, use the following:
newMatrix = reshape(matrix(:,1),[],3);
Note that the above code uses [] in the second argument (the number of rows argument) to mean "automatically determine number of rows".This is automatically determined based on the number of columns, which is defined in the third argument here as 3. The reshape function requires that the number of output rows * output columns be equal to input rows * input columns. So in the above case this will only work if the starting matrix has a number of rows which is divisible by 3.

Matlab - show corresponding values of 1st column of matrix, baring in mind the last 2 values of the 2nd column

I created a matrix with two columns, using two vectors, f and PS, that I already had:
M = [f PS]; %81x2 matrix
And then I sorted the matrix with respect to "PS" (second column), in order to have the maximum values of "PS" at the last positions of the vector:
M1=sortrows(M,2); %81x2 matrix
And I got something like this:
f...PS
5...83
10...136
3...357
1...985
Since now I assured the last two values of "PS" are indeed the maximum values, the information I want to give to the user is 1 and 3 (f values corresponding to the 2 maximum values of PS, which are at the bottom).
How can I do this?
You can use the end index: M1(end,1) and M1(end-1,1) should contain 1 and 3, respectively.
Best,

Assigning the different row to another matrix after comparing two matrices

i have two matrices
r=10,000x2
q=10,000x2
i have to find out those rows of q which are one value or both values(as it is a two column matrix) different then r and allocate them in another matrix, right now i am trying this.i cannot use isequal because i want to know those rows
which are not equal this code gives me the individual elements not the complete rows different
can anyone help please
if r(:,:)~=q(:,:)
IN= find(registeredPts(:,:)~=q(:,:))
end
You can probably do this using ismember. Is this what you want? Here you get the values from q in rows that are different from r.
q=[1,2;3,4;5,6]
r=[1,2;3,5;5,6]
x = q(sum(ismember(q,r),2) < 2,:)
x =
3 4
What this do:
ismember creates an array with 1's in the positions where q == r, and 0 in the remaining positions. sum(.., 2) takes the column sum of each of these rows. If the sum is less than 2, that row is included in the new array.
Update
If the values might differ some due to floating point arithmetic, check out ismemberf from the file exchange. I haven't tested it myself, but it looks good.