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

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.

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.

How do I save the indices of the top 4 maximum numbers in a matrix in scilab

I need to save the indices of 4 maximum numbers
for example, I need to get the indices of rows 10,9,7,5
5.0259327
4.7127487
4.8435524
4.8538644
5.1048996
6.2441973
5.9413803
6.2912638
5.1117512
5.8309519
5.7419509
6.9663477
5.9958319
6.9519781
6.5802736
6.7327558
7.6765878
I have used
[mA,nA]=max(distA)
where mA is the row and nA is the column
in getting one maximum number but I cannot figure out how to choose another maximum number without duplication. I cannot sort because I need the indices.
You can use the gsort function:
[S,ind]=gsort(distA,"g","d");
The index of the 4 largest elements is the given by
ind(1:4)
You can use this little trick.
[output_val, output_index] = max(input_mat(input_mat < max(input_mat)))
This will give you the value and index of the second largest element. And then similarly, you can do it for 4 numbers.

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

Matlab : How to extract elements in a uniform interval?

I have a (5160 X 4) matrix.
I want to extract just (1,1),(41,1),(81,1),(121,1)........ in a uniform interval, only from the first column of the matrix.
Assuming that data is your matrix, you can do this:
A = data(1:40:5160,1);
The 1:40:5160 will create an array such that it starts at 1, and goes up in increments of 40 as much as possible up until 5160. Once you create 1:40:5160, you can use this array and access the corresponding rows, and you are accessing the first column using the index of 1 for the second parameter. Actually, the last row that gets extracted is 5121. We aren't able to go up to 5161 due to the fact that your matrix has 5160 rows, and we have also specified 5160 as the ending of the indexing.
NB: This is very basic MATLAB syntax. Any standard MATLAB tutorial should teach you this.

How do I find frequency of values appearing in all rows of a matrix in 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"