could anyone help me to solve the issue
조회 수: 1 (최근 30일)
이전 댓글 표시
I am having a matrix
A=[ 0 0 0 0.1677 2.0290 0 0 0.1844;
0.1348 0 0 0 0.1749 0 0 0;
2.2927 3.7844 2.2049 4.7058 5.2020 0.1800 6.2291 0;
3.3584 0 4.2332 0 0 0 0.1832 0;
0 0.1474 0 0 0 1.9069 1.7691 6.7248;
0 2.2580 0.1585 2.1291 0 5.7149 0 1.6265]
I want to rearrange the matrix in a manner that each of the rows can have one highest non zero value or two highest non zero value or three highest non zero value but not more than three.
Followd by if first row has the maximum value at its fifth place,then the rest of the rows should never choose the fifth place.
If secnd row contains the maximum values at fifth and first place then the second row should contain the value only at its first place and not at its fifth place.
could anyone please help me on this.
댓글 수: 2
Raj
2019년 8월 9일
Question is not quite clear. It would be helpful to everybody if you post the expected output for the A matrix that you have shown in your question.
답변 (3개)
Neuropragmatist
2019년 8월 9일
Its sounds like your question is:
Each row can only have maximum 3 numbers greater than zero and each row must have these maxima in different columns? Is that right?
What happens if a row has only zeros?
In the end can any columns contain only zeros?
Does the order matter? i.e. can the maxima for column 1 be in any row?
neil jerome
2019년 8월 9일
can't follow your question exactly; your expected output shows 2 answers in the top row, but these are not the highest values? (should be 2.029 and 1.844?).
anyway, try something like this - find the top nRes per row, then eliminate others in the row, and downwards through the matrix. 'sort' will arbitrarily return indices for zero, so you have to check for that before filling zeroes downwards. if this isn't exactly what you want, hopefully gives you a starting point..
nRows = size(A,1);
nRes = 2; % specify how many results to keep per row
for aa = 1:nRows
thisRow = A(aa,:); % this just makes the code clearer to follow
[~, ind] = sort(thisRow, 'descend'); % find locations of highest
A(aa,ind(nRes+1:end)) = 0; % zero out the rest of the row
A(aa+1:end, ind((thisRow(ind(1:nRes))) ~= 0) ) = 0; % zero below maxima
end
댓글 수: 2
neil jerome
2019년 8월 10일
like i say, this is really only a starting point given that your question isn't well defined (see also the other comments).supplying more detail in the question helps :) good luck!
Bruno Luong
2019년 8월 10일
편집: Bruno Luong
2019년 8월 10일
Ypu can use assignment optimal problem to solve it, one implementation is in FEX Munkres algorithm
A=[ 0 0 0 0.1677 2.0290 0 0 0.1844;
0.1348 0 0 0 0.1749 0 0 0;
2.2927 3.7844 2.2049 4.7058 5.2020 0.1800 6.2291 0;
3.3584 0 4.2332 0 0 0 0.1832 0;
0 0.1474 0 0 0 1.9069 1.7691 6.7248;
0 2.2580 0.1585 2.1291 0 5.7149 0 1.6265]
[m,n] = size(A);
[~,j] = mink(A,3,2);
i = repmat((1:m)',1,3);
B = accumarray([i(:),j(:)],1,[m n]);
c = assignmentoptimal(B); % FEX
B = accumarray([(1:m)',c],1,[m n]).*A
The result is:
B =
0 0 0 0.1677 0 0 0 0
0.1348 0 0 0 0 0 0 0
0 3.7844 0 0 0 0 0 0
0 0 4.2332 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 5.7149 0 0
댓글 수: 2
Bruno Luong
2019년 8월 11일
mink is available on more recent MATLAB version. It is quite simple to replace it. Do your own search on Answer for example.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!