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.
Related
I have a 133120x4 matrix in Matlab.
I would like to find the largest value in each row, and divide every element in that row by that particular value.
Do I need to use some sort of loop? For example: I find the amount of rows in that matrix (133120), and iterate the loop that amount of times, then I go row by row and use the max function to return the largest value in that row, and divide each element in that row by the returned value from max.
Or is there a quicker way of doing this?
Thanks
EDIT (for clarification):
lets call my 133120x4 matrix A. I want to divide every element in a row by the largest value in that row. Since max and element-division are vectorized, would the solution simply be:
A_normal = A / max(A)
resulting in a 133120x4 matrix, but within each row, the largest value would be 1.
Is this correct? EDIT: It is not correct, and am still trying to figure out solution. Help from the community is greatly appreciated
Compute the maximum with max, repeat the result N(=4) times so there is one per each element and then element wise division
!
newMat=mat./repmat(max(mat,[],2),[1 size(mat,2)]);]
or in R2016b or newer just
newMat=mat./max(mat,[],2);
My goal is to create a random, 20 by 5 array of integers, sort them by increasing order from top to bottom and from left to right, and then calculate the mean in each of the resulting 20 rows. This gives me a 1 by 20 array of the means. I then have to find the column whose mean is closest to 0. Here is my code so far:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
MeanArray= mean(transpose(NewArray(:,:)))
X=min(abs(x-0))
How can I store the column number whose mean is closest to 0 into a variable? I'm only about a month into coding so this probably seems like a very simple problem. Thanks
You're almost there. All you need is a find:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
% MeanArray= mean(transpose(NewArray(:,:))) %// gives means per row, not column
ColNum = find(abs(mean(NewArray,1))==min(abs(mean(NewArray,1)))); %// gives you the column number of the minimum
MeanColumn = RandomArray(:,ColNum);
find will give you the index of the entry where abs(mean(NewArray)), i.e. the absolute values of the mean per column equals the minimum of that same array, thus the index where the mean of the column is closest to 0.
Note that you don't need your MeanArray, as it transposes (which can be done by NewArray.', and then gives the mean per column, i.e. your old rows. I chucked everything in the find statement.
As suggested in the comment by Matthias W. it's faster to use the second output of min directly instead of a find:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
% MeanArray= mean(transpose(NewArray(:,:))) %// gives means per row, not column
[~,ColNum] = min(abs(mean(NewArray,1)));
MeanColumn = RandomArray(:,ColNum);
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))
Maybe I should just go with a for loop but I want to see if there is a more efficient/faster way to do it.
I have a matrix of numbers, let's say 10x10. I want to multiply 1,1 by 1,2, then 1,3 times 1,4, etc and then sum those results for row 1. Then move to the next row and do the same thing. The end result would be a vector of 10.
It is possible for this matrix to be 1000x1000 so I want it to be as fast as possible. Thanks!
I would use
v = sum(M(:,1:2:end-1).*M(:,2:2:end),2);
Here M(:,1:2:end-1).*M(:,2:2:end) does multiplication: every element of an odd-numbered column of M is multiplied by its neighbor to the right. (This assumes even number of columns, otherwise the process you described is ill-defined.) Then every row is added up by the sum command.
On my computer, doing this for a 1000 by 1000 matrix takes 0.04 seconds.
I have a 161*32 matrix (labelled "indpic") in MATLAB and I'm trying to find the frequency of a given number appearing in a row. So I think that I need to analyse each row separately for each value, but I'm incredibly unsure about how to go about this (I'm only new to MATLAB). This also means I'm incredibly useless with loops and whatnot as well.
Any help would be greatly appreciated!
If you want to count the number of times a specific number appears in each row, you can do this:
sum(indpic == val, 2)
where indpic is your matrix (e.g image) and val is the desired value to be counted.
Explanation: checking equality of each element with the value produces a boolean matrix with "1"s at the locations of the counted value. Summing each row (i.e summing along the 2nd dimension results in the desired column vector, where each element being equal to the number of times val is repeated in the corresponding row).
If you want to count how many times each value is repeated in your image, this is called a histogram, and you can use the histc command to achieve that. For example:
histc(indpic, 1:256)
counts how many times each value from 1 to 256 appears in image indpic.
Like this,
sum(indpic(rownum,:) == 7)
obviously change 7 to whatever.
You can just write
length(find(indpic(row_num,:)==some_value))
and it will give you the number of elements equal to "some_value" in the "row_num"th row in matrix "indpic"