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

50 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Joel Handy
Joel Handy el 18 de Jul. de 2019
Editada: Jan el 19 de Jul. de 2019
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 comentarios
Jon
Jon el 18 de Jul. de 2019
Editada: Jon el 18 de Jul. de 2019
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?
Mohammad Mahmoud
Mohammad Mahmoud el 18 de Jul. de 2019
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

Iniciar sesión para comentar.

Más respuestas (1)

Jon
Jon el 18 de Jul. de 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 comentarios
Jon
Jon el 18 de Jul. de 2019
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
Jon
Jon el 19 de Jul. de 2019
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)

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by