could anyone help me to solve the issue
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Raj
el 9 de Ag. de 2019
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.
Respuestas (3)
Neuropragmatist
el 9 de Ag. de 2019
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
el 9 de Ag. de 2019
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 comentarios
neil jerome
el 10 de Ag. de 2019
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
el 10 de Ag. de 2019
Editada: Bruno Luong
el 10 de Ag. de 2019
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 comentarios
Bruno Luong
el 11 de Ag. de 2019
mink is available on more recent MATLAB version. It is quite simple to replace it. Do your own search on Answer for example.
Ver también
Categorías
Más información sobre Parallel Computing Fundamentals en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!