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

14 ビュー (過去 30 日間)
Mohammad Mahmoud
Mohammad Mahmoud 2019 年 7 月 18 日
コメント済み: Jon 2019 年 7 月 19 日
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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by