필터 지우기
필터 지우기

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

조회 수: 15 (최근 30일)
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

채택된 답변

Joel Handy
Joel Handy 2019년 7월 18일
편집: Jan 2019년 7월 19일
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
Jon
Jon 2019년 7월 18일
편집: Jon 2019년 7월 18일
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 2019년 7월 18일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jon
Jon 2019년 7월 18일
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
Jon
Jon 2019년 7월 18일
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 2019년 7월 19일
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)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by