87 views (last 30 days)

Show older comments

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
on 18 Jul 2019

Edited: Jan
on 19 Jul 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))

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)

Jon
on 19 Jul 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)

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

Start Hunting!