matrix sorting and distibution
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
yousef Yousef
am 4 Mai 2014
Kommentiert: Geoff Hayes
am 5 Mai 2014
I have this matrix:
2 2 82 10 16 15 66 76 71 83 44 49
4 2 91 28 98 43 4 75 4 70 39 45
4 1 13 55 96 92 85 40 28 32 77 65
1 2 92 96 49 80 94 66 5 96 80 71
1 2 64 97 81 96 68 18 10 4 19 76
the code should look to first column,if its not repeated,gives the output:
8 1 6 7 5 10 9 3 4 2 which is the indices after sorting in descending way.
if its repeated,look to the second column if 1 ,the code should sort the elements of second line and distribute them as 1:7 to original and the rest to the repetition
if the of second column is 2 ,the code should divide 1:5 to first and 5:10 to the second.
the result should look like:
8 1 6 7 5 10 9 3 4 2
3 1 6 8 10 0 0 0 0 0
4 9 2 5 7 0 0 0 0 0
2 8 5 1 4 0 0 0 0 0
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 4 Mai 2014
yousef - the easiest part of the above problem is to get a matrix of sorted indices that correspond to the data in each row sorted in descending order. Consider using the sort command (type help sort in the command window for details). With it, you can sort as follows:
Z = [2 2 82 10…]; % your 5x12 input matrix
% ignore (~) sorted data and consider the indices (Idcs) only, sorting on all rows of Z but only
% on columns 3 through the end; we sort on the second dimension (2) which is the columns in
% descending order
>>[~,Idcs] = sort(Z(:,3:end),2,'descend');
>> Idcs
Idcs =
8 1 6 7 5 10 9 3 4 2
3 1 6 8 10 4 9 2 5 7
3 4 5 9 10 2 6 8 7 1
2 8 5 1 4 9 10 6 3 7
2 4 3 10 5 1 9 6 7 8
Now you can iterate over each element in the first row of Z, checking to see how many times that element is repeated in that first column (I think that is your requirement). You can cound the number of times an element is repeated by using a combination of the length and find commands (left to you). If not repeated, then do nothing since the row of Idcs is already sorted in descending order by indices. If it is repeated, then consider the second column and check for its repetition using length and find. If not repeated, then apply your first rule (which is a little fuzzy - why consider 1:7 and what is repetition? If it is repeated, then apply your second rule which is a little fuzzy too - are you leaving the first five indices in the current row, and moving the remaining five indices to the subsequent row? (Or that row which has the repeated second column element? Is that why your final answer has only four rows when the input has five?
5 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!