Frequency of maximum values occuring in columns - matlab

I have a 35 x 24 matrix of numbers. Each of the 35 rows obviously has a maximum value. I'm trying to write a short piece of code which determines which of the 24 columns contains the most of these max values. The catch is that no loops are allowed.
For example if the maximum values for 30 different rows all happened to lie in column 7 then I would want MATLAB to return the answer 7 since this is the column with the most max row values.

If the values in each row are unique, we can simply use the second output of max combined with the mode to figure out which column contains the maximum of each row most often.
% Find the column which contains the maximum value
[~, column] = max(data, [], 2);
result = mod(column);
However, below is a more general solution that allows a given maximum value to occur multiple times per row.
maximaPerColumn = sum(bsxfun(#eq, data, max(data, [], 2)), 1);
result = find(maximaPerColumn == max(maximaPerColumn));
Explanation
First we want compute the maximum value for each row (maximum across columns, dimension 2).
rowMaxima = max(data, [], 2);
Then we want to replace each row with a 1 if the value is equal to the max of that row and 0 otherwise. We can easily do this using bsxfun.
isMaxOfRow = bsxfun(#eq, data, rowMaxima);
Then we want to figure out how many times a given column contains a max of a row. We can simply sum down the columns to get this.
maximaPerColumn = sum(isMaxOfRow, 1);
Now we want to find the column which contained the maximum number of maxima. We use find due to the fact that more than one column could contain the same number of maxima.
result = find(maximaPerColumn == max(maximaPerColumn));

I think you are looking for this:
sum(A==max(A,[],2))
Example:
A = [1 1 1;
2 2 2;
3 2 1]
M = sum(A==max(A,[],2))
Returns:
[3 2 2]
The first column has the most row wise max values. You could use find to identify this column.
find(M==max(M))

Related

Find the row/column of all maximum values in each row of a matrix

I want search indexes maximum value/s in each row. If row has more than one maximum, so I want to save both indexes.
For example:
X = [5 6 8
1 2 3
4 4 0];
And I need indexes
inds = [1 3
2 3
3 1
3 2];
I wanted to use function max but this function only saves one index.
You can use max to compute the max for each row and then compare the elements in each row to it's row-wise max using bsxfun and eq. Then you can find the row/column positions of these maxima. We use a transpose in there (.') to ensure that we get the ordering of the output that you expect.
[c,r] = find(bsxfun(#eq, d, max(d, [], 2)).')
output = [r,c];
Another way to do it, would be to use max and repmat.
First you find the maximum of each row using
rowMaximum=max(X,[],2);
Then you replicate the maximum so that it has the same dimension as your input and compare it to the input
logicalMaximum=repmat(rowMaximum,1,size(X,2))==X;
And the last thing you wanna do is converting this logical array into your desired indexes
[columns,rows]=find(logicalMaximum);
result=[rows,columns];

how to pick put a specific row in matlab with the largest amount of numbers above a certain value

so I have an array in matlab with 61 rows and 181 columns. I want to find the row or column with the largest amount of numbers above a certain value and then count those numbers. Some guidance would be great.
Say your matrix is M
First lets create a new matrix MThresh which will indicate which number is greater than a threshold thresh
MThresh = M>Thresh;
Now, in order to know which row has the most of them we just need to sum all of the columns (MThresh contains 1 where it is larger and 0 where it is not).
N = sum(MThresh,2);
This is a column vector. Now we just have to find the maximum. The row is what we want here.
[Howmany Row] = max(N);
You can do it all in a single line of code:
[Howmany Row] = max(sum(M>Thresh,2));

How to plot numeric values in a column when values in another column are equal to specific number

How can I get the numeric values in a column (let's say column 10) when the numeric values in another column (let's say column 9) are equal to a specific number and plot this in a graph.
e.g., When values of column 9 == 4, get the corresponding value of column 10 and plot. I am using row index number as a marker for time.
I am plotting all of column 10 to get a waveform then I want to use the data of column 9 to add markers to my waveform that are representative of a command occurring at a certain point in time.
Here is my code:
E = csvread('Experiment_at_10_45_1.csv');
[signal_rows, signal_columns] = size(E);
t=(1:signal_rows)/128; %128 samples per second
%% SNR plot for down frequency
plot(t,E(:,13),'k')
I hope my explanation is clear, as I have attempted to use a minimum working example of my code for the first time.
You'll want to use logical indexing to do this. You want to first create an array of 0 (false) and 1 (true) values where column 9 is equal to the value you want.
bool = E(:,9) == 4;
Then you'll want to use this 0 and 1 array as the row index. This will grab only the rows where column 9 was equal to 4. This is referred to as logical indexing.
E(bool, 10)
Then you can plot this
plot(t(bool), E(bool, 10))
As pointed out though, it is possible that the values aren't exactly to 4 due to floating point representation. To get around this, you just want to check if they are "close enough" using a very small epsilon.
bool = abs(E(:,9) - 4) < 1e-12;

How to read the indices of my max value in any given array using a created function

I have created a function that should be able to read in any mxn matrix and gives me the maximum value of the entire matrix (not just per column) and what its indices are.
function [ BIGGEST ] = singlemax( x )
[Largest_values row]=max(x)
[biggest_number column] = max(max(x))
end
This function gives me all the information I need, however it is not very clean as it gets messy the larger the matrix.
The real problem area is printing out the row in which the maxima is located.
Largest_values =
0.7750 0.9122 0.7672 0.9500 0.6871
row =
3 2 3 2 2
biggest_number =
0.9500
column =
4
This is my print out given a random matrix as an input.With the function I have created I cannot read the indices of my max value in any given array using a created function. If I could somehow relate the maximas from each column and there corresponding row (such as making the results a matrix with the column max on top and the row index on bottom, all within the same respective columns )I could display the row of the absolute maximum.
Here's one approach:
value = max(x(:));
[rowIndex,columnIndex] = ind2sub(size(x),find(x==value));
Read the ind2sub documentation for more details.
Edited to modify so that it finds indices of all occurrences of the maximum value.

Selecting certain numbers based on corresponding numbers of other column in MatLab

I am a newcomer struggling with writing a code in MatLab which is extremely useful for my research. I have csv files with many columns, but interested in only two of them, say columns X and Y, all numbers. What I need to do is picking certain number from X column having approximate corresponding number in Y column. For example, picking a number in X column that has closest-to-54 corresponding Y. Can I do it with MatLab?
I appreciate any help.
Suppose you have your matrix data like this:
data =
0.6293 -2.5788 -0.9027 53.0100 -0.7203
0.3602 2.0267 0.2537 54.5300 -0.0149
0.0870 -0.7028 1.2209 54.6034 0.5088
-1.1552 1.5505 0.3589 54.0044 1.1467
1.7230 -0.5437 0.1975 56.3978 -0.5802
1.9416 0.7137 0.2764 55.0810 -0.2298
0.4837 -0.1987 0.8526 53.6421 -0.4461
-0.1257 1.7165 -0.8384 53.7798 1.3852
0.6433 0.6902 -1.1979 53.9323 -1.2776
1.6593 -0.5343 -1.3725 52.8364 -0.0738
And you want to find the value on column X = 2 where the value on the same row, on column Y = 4, is closest to 54. You could do it like this:
X = 2; % Index of the column to retrieve the value from
Y = 4; % Index of the column to find the value nearest to 54
[~,idx] = min(abs(data(:,Y) - 54))
data(idx,X)
Which would give you this:
ans =
1.5505
What it's doing is taking the absolute difference between column Y and 54 and retrieving the index of the row of the element with minimum difference. Then, this index is used to retrieve the value of that line, on column X.