MATLAB Answers

0

Finding a max in a column and making all other elements equal to zero

Asked by Mohammad Mahmoud on 18 Jul 2019
Latest activity Commented on by Jon
on 19 Jul 2019
I have a 5x20 matrix and i want to
1) find the max value in each column
2) make all other values in the column zero except for max
3) count the number of non-zero elements in each row
4) show the number of row that has maximum non zero element
for example
a= [ 1 3 3
3 1 2
2 1 1 ]
a= [ 0 3 3
3 0 0
0 0 0 ]
row 1 has max number of non zero elements

  0 Comments

Sign in to comment.

2 Answers

Answer by Joel Handy on 18 Jul 2019
Edited by Jan
on 19 Jul 2019
 Accepted Answer

a= [ 1 3 3; 3 1 2; 2 1 1 ]
numRows = size(a,1);
maxvals = max(a)
a(a~=repmat(max(a),numRows,1)) = 0
numNonZeroInCol = sum(a~=0)
numNonZeroRow = sum(a~=0,2)
mostMaxIdx = find(numNonZeroRow == max(numNonZeroRow))

  3 Comments

Thank you so much this really helped I have another question though
I changed the last syntax to find the minumum value as such
mostMinIdx = find(numNonZeroRow == min(numNonZeroRow))
Which gave me two row numbers with the same lowest number of elements
how can i pick the lowest element value of the two
If i have for example
a = [ 3 0 0 0]
0 2 0 2
0 0 0 1]
mostMinIdx = 1
3
how can i choose the row with lowest element value ( the third row with the 1 )
and whats the syntax for having two or more elements in the a row and choose the lowest of them
It isn't clear to me what you are trying to find. Are you trying to find the row that has the most occurences of the column minimum?
no im trying to find the row of that has the least element value
if a 2 rows has 1 element each i want to choose the row that has the lowest value element

Sign in to comment.


Answer by Jon
on 18 Jul 2019

I see that as I was working on this Joel had already given you an answer, but here is another approach in case it is useful
% test matrix
A = [ 1 3 3;3 1 2; 2 1 1];
% define second matrix to hold the max values, otherwise zero
B = zeros(size(A));
% find the column max values and location (using linear indexing) where they occur
[M,I] = max(A,[],1,'linear');
% assign the max values at the locations where they occur
B(I) = M
% for each row, find the number of instances where the max value occurs
% (these are the nonzero entries in B)
count = sum(B~=0,2) % note set dim parameter to 2 to get row sums
% determine which row has the most occurences
[~,maxCountRow] = max(count)

  7 Comments

Regarding your second question, I think you want to find out which row has the most occurences of the column minimum. If so this code, which is similar to the other will do this. Note that I set the values that are not equal to the column minimum to NaN (Not a Number) rather than zero as the minimum value itself might be zero, and then it wouldn't get counted
% find the column min values
M = min(A);
% assign the min values at the locations where they occur
% define second matrix to hold the min values, otherwise NaN
B = A;
B(A~=M) = NaN
% for each row, find the number of instances where the min value occurs
% (these are the finite entries in B)
count = sum(isfinite(B),2) % note set dim parameter to 2 to get row sums
% determine which row has the most occurences
[~,maxCountRow] = max(count)
You asked - IS there a command to find the non-zero indices in a specific row ?
To find the non-zero indices in the ith row use
find(A(i,:)~=0)
A(i,:) , the indices i,: tells MATLAB to use the ith row, every column
Regarding your follow up request, I am having some difficulty understanding what you are looking for, but I think you want to find the row that has the least occurences of the column maximum. If this is not unique, that is there is more than one such row, then choose the row whose row minimum is the smallest. Finally I will note that this may itself not be unique. In this case I guess you would arbitrarily choose one of the ones whose row minimum is the smallest.
I have appended some code to my earlier post which will accomplish this ( for completeness I have included the whole script)
% find the column max values
M = max(A);
% assign the max values at the locations where they occur
% define second matrix to hold the max values, otherwise zero
B = A;
B(A~=M) = 0
% for each row, find the number of instances where the max value occurs
% (these are the nonzero entries in B)
count = sum(B~=0,2) % note set dim parameter to 2 to get row sums
% determine which row has the most occurences
[~,maxCountRow] = max(count)
% determine which rows have the least occurences of the maximum
% can't just use second
% left hand argument to min, as there may be more than one with the same
% count
idlCnt= min(count)==count % will be true for rows where minimum occurs
% find rows with smallest row minimum
rowMin = min(A,[],2)
idlSrm = rowMin == min(rowMin)
% find row(s) with the smallest number of maximums and the smallest row
% minimum
iLeastCount = find(idlCnt&idlSrm)
% there may be more than one row that meets this criteria, arbitrarily pick
% the first one
iLeastCount = iLeastCount(1)

Sign in to comment.