could anyone help me to solve the issue

3 ビュー (過去 30 日間)
jaah navi
jaah navi 2019 年 8 月 9 日
コメント済み: jaah navi 2019 年 8 月 12 日
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
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.
jaah navi
jaah navi 2019 年 8 月 9 日
[0 0 0 0.1677 2.0290 0 0 0;
0.1348 0 0 0 0 0 0 0;
0 3.7844 0 0 0 0 6.2291 0;
0 0 4.2332 0 0 0 0 0;
0 0 0 0 0 0 0 6.7248;
0 0 0 0 0 5.7149 0 0]
this was the expected output which i require.

サインインしてコメントする。

回答 (3 件)

Neuropragmatist
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?
  1 件のコメント
jaah navi
jaah navi 2019 年 8 月 12 日
with respect to your first question-
1.Each row can only have maximum 3 numbers greater than zero and each row must have these maxima in different columns? Is that right?
yes.right.
2.What happens if a row has only zeros?-
with respect to the input matrix as shown
[ 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]
none of the rows has no zeros.
3.In the end can any columns contain only zeros?-
Each column should contain atleast one value.
4.Does the order matter? i.e. can the maxima for column 1 be in any row?
yes.the maxima for column 1 can be in any of the row.

サインインしてコメントする。


neil jerome
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 件のコメント
jaah navi
jaah navi 2019 年 8 月 10 日
when i run the code the last row doesnot contain any non zero values.
neil jerome
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
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 件のコメント
jaah navi
jaah navi 2019 年 8 月 11 日
when i run the code i am getting error stating undefined function or variable mink.
Bruno Luong
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 ExchangeParallel Computing Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by