How do I find frequency of values appearing in all rows of a matrix in MATLAB? - matlab

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"

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.

Find the largest value of each row in Matlab and divide each number in that row by the number

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);

MATLAB Extracting Column Number

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);

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.

Count the number of times a number is repeating in a vector

I have created a vector containing zeros and 1's using the following command in a for loop.
G(:,i)=rand(K,1)<rand;
Since this is part of a larger problem at a particular stage I need to count the number of 1's that are present in each column.
I have tried to find the count using a for loop which is very messy and takes too long.
I found that histc can be used for this but I get an error
histc(G(:,1),1)
First input must be non-sparse numeric array.
Is there a better way to do this or am I missing something here ?
If you have a matrix G containing zeroes and ones, and you want to know how many ones are in each column, all you need is SUM:
nZeroes = sum(G);
This will give you a vector containing a total for each column in G.